MaxLength attribute on EditText not working as expected across xml and java

This may have been answered already, but I cannot find it on SO.

I have a very simple requirement: Limit the length of characters in the EditText.

I am using tag maxLength

in xml for this. The weird thing is that if the length of the entered text exceeds the specified limit, the text cannot be deleted by pressing the backspace with the soft keyboard. The entered text remains in the EditText field. After closing and reopening the softkey, the text becomes "deleteable".

<RelativeLayout xmlns:android=""



Adding this layout is enough to get the described behavior.

I added an InputFilter to see what's going on here. The result, as described, will be the same without the InputFilter!

protected void onCreate(Bundle savedInstanceState) {
    EditText et = (EditText) findViewById(;

    InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source,
                int start,
                int end,
                Spanned dest,
                int dstart,
                int dend) {

            if (source != null) {
                Log.v("edit","source = " + source.toString());
                Log.v("edit","start = " + start);
                Log.v("edit","end = " + end);
            if (dest != null) { 
                Log.v("edit","dest= " + dest.toString());
                Log.v("edit","dstart = " + dstart);
                Log.v("edit","dend = " + dend);
            return null;

    InputFilter[] oldFilters = et.getFilters();
    InputFilter[] newFilters = new InputFilter[oldFilters.length + 1];
    System.arraycopy(oldFilters, 0, newFilters, 0, oldFilters.length);
    newFilters[oldFilters.length] = filter;


During check-in, I see it source

will remain the same if the value is maxLength

exceeded, but Spanned dest

will change the way it should, by removing the text characters by pressing backspace.

Now what's going on here? This is a bug or indeed the default behavior. Because if the user enters their text and the limit is exceeded, they have no chance of being corrected.


Sandeep Kumar's suggested method using Java code to set maxLength produces the same result.

 et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});


By the way, I am using Android API 4.4.2 and SDK 23.0.2.


source to share

2 answers

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


if this code is helpful please vote up!



What worked for me was to use the bounding Textwatcher as below and use textNoSuggestions as inputType

public class LengthTextWatcher implements TextWatcher {

    String prevVal = "";
    int length;
    public LengthTextWatcher(int l) {
        length = l;
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    public void afterTextChanged(Editable s) {
        if(s != null && s.length() > 0) {
            if(s.length() > length) {
                s.insert(0, prevVal);
            prevVal = s.toString();



An example of how to use a textWatcher

mFirstNameET.addTextChangedListener(new LengthTextWatcher(35));







All Articles