Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText maxLength property not applied properly when setFilters is used

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;
       }
like image 346
user1242611 Avatar asked Sep 07 '12 10:09

user1242611


3 Answers

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);
like image 190
Jozua Avatar answered Nov 19 '22 00:11

Jozua


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 });
}
}
like image 41
android_dev Avatar answered Nov 19 '22 01:11

android_dev


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});

like image 1
Mahmud Avatar answered Nov 19 '22 02:11

Mahmud