Android:: Set max-length of EditText programmatically with other InputFilter

Android

Android Problem Overview


I'm using InputFilter like this to allow only alpha and numbers

private InputFilter[] inputFilters = new InputFilter[] { new InputFilter()
{
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
    {
        for (int i = start; i < end; ++i)
        {
            if (!Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]*").matcher(String.valueOf(source.charAt(i))).matches())
            {
                return "";
            }
        }

        return null;
    }
} };

But problem is "android:maxLength" value in xml file is not working with this InputFilter

I think I need code in InputFilter to set max-length of EditText

Anyone has good idea for this?

Thanks

Android Solutions


Solution 1 - Android

A simple one-liner would be:

myEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(10) });

//replace 10 with required length.

Solution 2 - Android

public void setEditTextMaxLength(int length) {
    InputFilter[] filterArray = new InputFilter[1];
    filterArray[0] = new InputFilter.LengthFilter(length);
    edt_text.setFilters(filterArray);
}

Solution 3 - Android

Just try this way

InputFilter

InputFilter filter = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; ++i)
            {
                if (!Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]*").matcher(String.valueOf(source.charAt(i))).matches())
                {
                    return "";
                }
            }

            return null;
        }
    };

How to apply

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
        EditText edt =(EditText)findViewById(R.id.edt) ;

        edt.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(10)});


	}

Solution 4 - Android

> For Kotlin > > myEditText.filters = arrayOf(InputFilter.LengthFilter(maxLength))

Solution 5 - Android

If we want to limit the character input in an EditText , EditText in XML layout gives us android:maxLength to do this. But in java codes you might wonder why there isn't any setMaxLength(int length) function. The reason behind this is that when you want to restrict the EditText to accept certain value, you have to filter them and this would be invoked by setFilters. To make our EditText to have a fixed size we can use the following code.

EditText et = new EditText(this);
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
et.setFilters(FilterArray);

Solution 6 - Android

Building on @Deepak's I used the following:

public void setEditTextMaxLength(final EditText editText, int length) {
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(length);
    editText.setFilters(FilterArray);
}

Solution 7 - Android

in kotlin

textInputEditText.filters = arrayOf(InputFilter.LengthFilter(10))

Solution 8 - Android

et.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(20))

in kotlin

Solution 9 - Android

You can use this function very very easy :

 public static void setEditTextMaxLength(EditText editText, int length) {
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(length);
    editText.setFilters(FilterArray);
  }

and use this on your activity:

setEditTextMaxLength(edt_passcode, 5);

Solution 10 - Android

Use this method:

private void setEditTextMaxLength(EditText et, int maxLength) {
    et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpokosoView Question on Stackoverflow
Solution 1 - AndroidDivins MathewView Answer on Stackoverflow
Solution 2 - AndroidDeepakView Answer on Stackoverflow
Solution 3 - AndroidBiraj ZalavadiaView Answer on Stackoverflow
Solution 4 - AndroidBiplob DasView Answer on Stackoverflow
Solution 5 - AndroidIntelliJ AmiyaView Answer on Stackoverflow
Solution 6 - AndroidRay HunterView Answer on Stackoverflow
Solution 7 - AndroidSyed ZeeshanView Answer on Stackoverflow
Solution 8 - AndroidArchkWayView Answer on Stackoverflow
Solution 9 - AndroidMilad MohamadiView Answer on Stackoverflow
Solution 10 - AndroidAlireza NooraliView Answer on Stackoverflow