-3

I want the user to enter only alphabets and special characters in an EditText. One approach would be to add all the alphabets and all the special characters in the EditText's "android:digits" tag. But I don't think that a good way.

Any better way to achieve it?

Aditya Gupta
  • 508
  • 3
  • 18

3 Answers3

0

Check out EditText input type docs to find if any of these satisfy your requirement.

If not , using regular expressions to check input string from EditText is according to your required format or not is a good idea.

Sash_KP
  • 5,551
  • 2
  • 25
  • 34
0

I used a InputFilter to block out only numbers :

private static InputFilter filter = new InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String blockCharacterSet = "1234567890";
        if (source != null && blockCharacterSet.contains(("" + source))) {
            return "";
        }
        return null;
    }
};

Then Applying the filter :

edittext.setFilters(new InputFilter[] { filter });
Aditya Gupta
  • 508
  • 3
  • 18
0

Set android:digits="abcdefghijklmnopqrstuvwxyz?/>,<.:";'{}[]\|)(*&^%$#@!~+=-_ etc"` attribute in edittext inside your xml. Note:- please add all available special characters in keyboard in place of etc.

Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46
  • it's not working: I am getting Error parsing XML: not well-formed (invalid token). I want to allow only limited characters android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-/&^%$#@!" If I uses android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-/" then it works but I want to allow character &^%$#@! also from XML in Android. – Faiz Anwar Sep 18 '20 at 06:05
  • I have a requirement where I need to allow only ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-/&^%$#@! character from soft input key – Faiz Anwar Sep 18 '20 at 06:12