Salmaan Salmaan - 1 year ago 232
Android Question

android EditText maxLength not working

This is my xml

android:hint="Provide comments here..."

Neither is it working using this code

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

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download