androidandroid-edittextandroid-input-filter

Block numeric in EditText android using input filter


I want to do it programmatically by input filter not in xml like e.g - android:digits="aA".

Here is my code

public static void RemoveCharacter(EditText text, String character, int length) {
final String blockCharacterSet = character;
// final String blockCharacterSet = "\"+[]&~#^|$%*!@/()-'\\\":;,?{}=!$^';,?×÷<>{}€£¥₩%~`¤♡♥_|《》¡¿°•○●□■◇◆♧♣▲▼▶◀↑↓←→☆★▪:-);-):-D:-(:'(:O\";
InputFilter[] filter1 = new InputFilter[2];
filter1[0] = new InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        if (source != null && blockCharacterSet.contains(("" + source))) {
            return "";
        }
        return null;
    }
};
filter1[1] = new InputFilter.LengthFilter(length);


text.setFilters(filter1);

}

It is completely working in Moto G,nexus devices but not working in Samsung devices.


Solution

  • Well, if you really want to do it with InputFilter it will be a little bit more complex than that.

    Different keyboards act really differently, so i my guess is your solution won't be working with Swype/Swift keyboards.

    Probably try using something like this:

        final Set<Character> blockSet = new HashSet<>();
        blockSet.addAll(Arrays.asList('1', '2', '3', '4','5', '6', '7', '8', '9', '0'));
    
        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 (Character.isDigit(source.charAt(i))) {
                        char[] v = new char[end - start];
                        TextUtils.getChars(source, start, end, v, 0);
                        String s = new String(v);
    
                        if (source instanceof Spanned) {
                            CharSequence sp = new SpannableString(s);
                            TextUtils.copySpansFrom((Spanned) source,
                                    start, end, null, (Spannable) sp, 0);
    
                            boolean containsDigit = true;
                            while (containsDigit) {
                                containsDigit = false;
                                for (int j = 0; j < sp.length(); j++) {
                                    if (blockSet.contains(sp.charAt(j))) {
                                        sp = TextUtils.concat(sp.subSequence(0, j), sp.subSequence(j + 1, sp.length()));
                                        containsDigit = true;
                                        break;
                                    }
                                }
                            }
    
                            return sp;
                        } else {
                            s = s.replaceAll("\\d", "");
                            return s;
                        }
                    }
                }
                return null; // keep original
            }
        };