android EditText maxLength not working

18.8k views Asked by At

This is my xml

<EditText
android:id="@+id/et_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|textVisiblePassword"
android:hint="Provide comments here..."
android:gravity="top"
android:maxLength="5"
android:textSize="12sp"
android:visibility="visible"
/>

Neither is it working using this code

TextView editEntryView = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(5);
editEntryView.setFilters(filterArray);

maxLenth is not working, I dont know why, but it isnt.
I have checked other answers on the stack but they are also not working.
Please check if any of EditText attributes are conflicting or what is the problem?

EDIT: Same problem is being faced by other developers
See comments here same problem is being faced by Mansi and aat
And here in comments same problem is being faced by Vincy and Riser

EDIT: Problem solved
I was using input filter which overrides the max length in xml making it not able to work.
The reason input filter didn't worked for me was that I was using another input filter which overwrites the previous maxLength input filter.
Making it into a single input filter fixed that issue for me.

8

There are 8 answers

0
Armando On BEST ANSWER

Fairly old post but, I noticed how the XML is an actual EditText object, while you are adding the filters to a TextView which could handle it differently than EditText. If you are adding an InputFilter object manually, the xml property is overridden.

The example code on which you add InputFilters to the View seems to be a TextView object. Make sure you pull the right view and it's being cast to EditText if you go with the manual addition of the filters--it's working for me right now.

Good luck.

0
live-love On

If you already have InputFilter then maxLength will not work. You will have to create an additional InputFilter and add it:

    // Filter for comma character
    String blockCharacterSet = ",";
    InputFilter filter = (source, start, end, dest, dstart, dend) -> {
        if (source != null && blockCharacterSet.contains(("" + source))) {
            return "";
        }
        return null;
    };

    // Filter for max Length
    InputFilter filter1 = new InputFilter.LengthFilter(20);

    // Set the filters
    et_list_name.setFilters(new InputFilter[] { filter, filter1 });
0
CoolMind On

In my case DataBinding is used, but someone changed filter right in code using ViewBinding.

binding.edit_text.filters = arrayOf<InputFilter>(object : InputFilter {
    override fun filter(
        source: CharSequence?,
        start: Int,
        end: Int,
        dest: Spanned?,
        dstart: Int,
        dend: Int
    ): CharSequence {
        return source.toString().filter { it.toString().matches(("[${LETTERS}]").toRegex()) }
    }
})

Android Studio didn't find what method referred to "@+id/edit_text" until I removed that id from the EditText.

0
Daniil Pavlenko On

If you are using InputFilter for the edittext, then maxLength will not work.

It's correct if you add a filter with replacing original filters.

You can use android:maxLength="5" in your xml.

And in your code just add a filter without replacing existed filters.

Kotlin:

editText.filters = arrayOf(
  *editText.filters,
  InputFilter.AllCaps(), // your filter
  // other filters
)
3
Surya V On

Try this, it will work for both maxlenght and input filter

month.setFilters(new InputFilter[]{new InputFilterMinMax("0", "12"), new InputFilter.LengthFilter(2)});
0
Tripaty Sahu On

If you are using InputFilter for the edittext, then maxLength will not work.

0
Prajwal Waingankar On

To add character restriction as well as input limit.

   private String BlockCharacterSet_Name = "\\@$#!=><&^*+\"\'";   

 mFirstName.setFilters(new InputFilter[] {new InputFilter.LengthFilter(25),inputFilter_Name});



 private InputFilter inputFilter_Name = new InputFilter() { //filter input for name fields
        @Override
        public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
            if (charSequence != null && BlockCharacterSet_Name.contains(("" + charSequence))) {
                return "";
            }
            return null;
        }
    };

{new InputFilter.LengthFilter(25),inputFilter_Name}

allows you to set both max length limitation as well as your special characters restrictions both in the same InputFilter and therefore you dont need to create separate Inputfilter for the same.

0
Amar Jain On

Try this:

val existingFilters: Array<InputFilter> = editText.filters
editText.filters = existingFilters.apply { plus(filter) }

You need to get the list of existing filters like maxLength applied through XML and then add more filters to it. In your case, the maxLength property of the editText was overridden by the custom InputFilter you were trying to apply.