The EditText maxLength property is not applied properly when using setFilters

When I use the setFilter method for EditText to handle special characters, the maxLength property does not work as expected. My code is below.

 editName = (EditText)findViewById(R.id.rna_editTextName); editName.setFilters(new InputFilter[]{getFilteredChars()}); //Below method returns filtered characters. public InputFilter getFilteredChars() { InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (end > start) { char[] acceptedChars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ' ,'.', '\''}; for (int index = start; index < end; index++) { if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) { return ""; } } } return null; } }; return filter; } 
+4
android android-edittext input-filter
Sep 07
source share
4 answers

This is because the maxLength property sets the InputFilter to an EditText . By calling EditText.setFilters(new InputFilter[] {<YOUR_FILTER>}) , you override all existing InputFilters, including those used by maxLength .

To fix this, copy the array returned by EditText.getFilters() and add it to it:

 InputFilter[] editFilters = edit.getFilters(); InputFilter[] newFilters = new InputFilter[editFilters.length + 1]; System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length); newFilters[editFilters.length] = <YOUR_FILTER>; edit.setFilters(newFilters); 
+14
Sep 21 '13 at 15:59
source share

Try this code. Based on @Jozua's answer

  /** * Adds filter to EditText preserving other filters. * * @param editText * @param filter */ public static void setFilter(EditText editText, InputFilter filter) { InputFilter curFilters[] = editText.getFilters(); if (curFilters != null) { InputFilter newFilters[] = new InputFilter[curFilters.length + 1]; System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length); newFilters[curFilters.length] = filter; editText.setFilters(newFilters); } else { editText.setFilters(new InputFilter[] { filter }); } } 
+3
Jul 07 '14 at 13:54 on
source share

You can add multiple filters to the array. Like the maximum character limit and a special character filter in your text editor.

editText.setFilters (new InputFilter [] {new InputFilter.LengthFilter (MAX_CHARACTER_LIMIT), SpecialCharacterInputFilter});

+1
Feb 06 '16 at 9:22
source share

while setting maxlenth's edittext property

please use the code.

Wild Guess: The problem may be that you are setting maxLength in the layout. By calling setFilters (), this behavior is replaced with one of your filters.

Solution: use more than one filter or implement the maxLenght behavior in your getFilteredChars () filter.

EDIT: you can look at http://developer.android.com/reference/android/text/InputFilter.LengthFilter.html

and for you question in the comment, from the document:

 when the buffer is going to replace the range dstart … dend of dest with the new text from the range startend of source 

So something like (Pseudo code liveCoding):

 dest.lenght - (dend-dstart) + (end-start) = new legnth 
0
Jun 25 '13 at 8:54
source share



All Articles