Skip to content Skip to sidebar Skip to footer

EditText - Allow Only Specific Characters Known From Context

In my app i have EditText. I want user to be able to insert only digits and two characters specified by me. I know there exists android:digits='...' but I can't use this because i

Solution 1:

This is the proper way to do such task

InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 
                        // Your condition here 
                        if (!Character.isLetterOrDigit(source.charAt(i))) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
}; 

edit.setFilters(new InputFilter[]{filter}); 

Solution 2:


Post a Comment for "EditText - Allow Only Specific Characters Known From Context"