EditText maxLength property not applied properly when setFilters is used
Asked Answered
S

4

5

When I use setFilter method on an EditText to handle special characters, maxLength property is not working 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;
       }
Stupidity answered 7/9, 2012 at 10:58 Comment(4)
Define "not working as expected"Pachalic
i wanted to handle special character issue on edittext gor that i added above method i.e. getfilteredChars(). By using this method special character issue is handled but becoz of that edittext is accepting unlimited characters. while setting maxlenth property of edittext programatically we use InputFilters and here also in getfilters we r using Inputfilters so i think that becoz of that maxlenth property is nt working. So tell me solution to this problem.Stupidity
any solution for the problemBabettebabeuf
till i didn't get any solution.Stupidity
O
14

This is because the maxLength property sets an InputFilter on your EditText. By calling EditText.setFilters(new InputFilter[] {<YOUR_FILTER>}) you are overriding all existing InputFilters including the one used by maxLength.

To fix this, copy the array returned by EditText.getFilters() and add your own 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);
Ostiole answered 21/9, 2013 at 15:59 Comment(0)
L
5

Try this code. Based on @Jozua 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 });
}
}
Ledesma answered 7/7, 2014 at 13:54 Comment(0)
E
1

You may add multiple filter at array. Like maximum character limit and special character filter at your editText.

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

Eclat answered 6/2, 2016 at 9:22 Comment(0)
S
0

while setting maxlenth property of edittext programatically

please show use the code.

Wild Guess: the problem may be that you set the maxLength in the layout. By calling setFilters() this behavior is replace by the one of your Filter.

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

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

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

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

so, something like (Pseudo code liveCoding) :

dest.lenght - (dend-dstart) + (end-start)  = new legnth
Stickinthemud answered 25/6, 2013 at 8:54 Comment(1)
But how to implement maxLenght behavior in your getFilteredChars() filter?please explain.Stupidity

© 2022 - 2024 — McMap. All rights reserved.