How to set maximal length of EditTextPreference of AndroidX Library?
Asked Answered
P

3

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?

Peebles answered 14/5, 2019 at 10:59 Comment(2)
Have you used InputFilter filter on EdiText?Lettielettish
Awesome. We were able to define layout properties in xml file, now we have to patch them in source code. I am wasting hours on 'used to work' things. And code gets shittier in each iteration.Chops
D
6

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.

Dollie answered 4/9, 2019 at 13:1 Comment(2)
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()Chops
For how it's done, see answer here: https://mcmap.net/q/134992/-in-android-edittext-how-to-force-writing-uppercaseChops
L
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.

Lettielettish answered 14/5, 2019 at 11:16 Comment(4)
I'm sorry but I use EditTextPreference instead of EditText, so above code won't work.Peebles
Okay, Check this answer,Lettielettish
That's a different case. What I want is to limit the length while in input, not the value.Peebles
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. A shorter Kotlin version is also found there. Should limit the length while in input as you want.Ire
G
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()

Golanka answered 14/12, 2020 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.