0

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.

1
  • Please note that asking volunteers for urgency is usually a reliable downvote attractor - remember that it is only urgent for you, and readers will answer questions at their leisure. Commented Jun 17, 2016 at 16:16

2 Answers 2

1

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
        }
    };
Sign up to request clarification or add additional context in comments.

Comments

1

Try adding your choice of input type using below code, it will work:

your_editText.setInputType(InputType.TYPE_CLASS_TEXT | 
    InputType.TYPE_TEXT_VARIATION_PASSWORD);

1 Comment

Thanks Yaar Kaushik but its still not working for samsung devices..:(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.