5

Recently, I migrate my android project to AndroidX and I use the EditTextPreference from AndroidX library. Now, I want to set the maximum length of the EditTextPreference to let say 50. I have tried to use:

android:maxLength="50"

but it's not working.

It seems that all android namespace won't work with the EditTextPreference and there is no code suggestion so I cannot find any related code to set the maximum length. How can I set the maximum length?

3 Answers3

5

You need find your EditTextPreference by key, then set onBindEditTextListener to it and change layout attributes at the onBindEditText method:

EditTextPreference preference = findPreference("edit_text_preference_key");
    preference.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
        @Override
        public void onBindEditText(@NonNull EditText editText) {
            editText.setInputType(InputType.TYPE_CLASS_NUMBER); // set only numbers allowed to input
            editText.selectAll(); // select all text
            int maxLength = 2;
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)}); // set maxLength to 2
        }
    });

You can put this code to onResume() method of yout PreferencesFragment or PreferencesActivity.

tadiuzzz
  • 126
  • 2
  • 9
0

You may try with java code, that will works. Here is snipped.

EditText et = (EditText) findViewById(R.id.myeditText);
et.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(50) }); // maximum length is 50

I hope that will help you.

Mehul Solanki
  • 1,147
  • 11
  • 15
  • 1
    I'm sorry but I use EditTextPreference instead of EditText, so above code won't work. – Bernhard Josephus May 14 '19 at 11:31
  • Okay, Check [this](https://stackoverflow.com/questions/25166068/how-to-limit-edittextpreference-to-a-range-102465535/25169934) answer, – Mehul Solanki May 14 '19 at 11:35
  • That's a different case. What I want is to limit the length while in input, not the value. – Bernhard Josephus May 14 '19 at 11:44
  • This suggestion is correct, though you wouldn't use `findViewById()` to find the `EditText`. Instead, you'd first do `EditTextPreference myPref = findPreference("myPrefKey")` then if myPref wasn't null, you'd do something like `myPref.setOnBindEditTextListener(editText -> { editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(MAX_CHARS}); });}` This will clobber other `InputFilters`- you can find how to preserve them [here](https://stackoverflow.com/a/46450839/3035127). A shorter Kotlin version is also found there. Should limit the length while in input as you want. – fattire Feb 21 '20 at 07:02
0

This is the code to set maximal length (in this case 10) of EditTextPreference:

final EditTextPreference prefCustomText = findPreference(ActivityPreferences.PREF_DISPLAY_CUSTOM_TEXT);
prefCustomText.setOnBindEditTextListener(editText -> {
    editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
});

And I'm adding information as wrote @Viktor Brešan:

I think you should append the new InputFilter to ones that might have been added to EditText previously. You can get them by calling editText.getFilters()

t0m
  • 3,004
  • 31
  • 53