A note to people who are already using a custom input filter and also want to limit the max length:
When you assign input filters in code all previously set input filters are cleared, including one set with android:maxLength. I found this out when attempting to use a custom input filter to prevent the use of some characters that we don't allow in a password field. After setting that filter with setFilters the maxLength was no longer observed. The solution was to set maxLength and my custom filter together programmatically. Something like this:
myEditText.setFilters(new InputFilter[] {
new PasswordCharFilter(), new InputFilter.LengthFilter(20)
});
Xml
Java:
Kotlin:
A note to people who are already using a custom input filter and also want to limit the max length:
When you assign input filters in code all previously set input filters are cleared, including one set with
android:maxLength
. I found this out when attempting to use a custom input filter to prevent the use of some characters that we don't allow in a password field. After setting that filter with setFilters the maxLength was no longer observed. The solution was to set maxLength and my custom filter together programmatically. Something like this:This works fine...
android:maxLength="10"
this will accept only
10
characters.Another way you can achieve this is by adding the following definition to the XML file:
This will limit the maximum length of the
EditText
widget to 6 characters.