Samah Ahmed
Samah Ahmed

Reputation: 419

Show only English characters when editing text in EditText in Android

I have 3 languages on my keyboard in android device and I want to show only English language letters and numbers when starting to edit text inside EditText like username field , I tried to use this line in xml file for Edittext but without any result.

android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"

Upvotes: 1

Views: 3168

Answers (1)

Amir Ziarati
Amir Ziarati

Reputation: 15097

the best way is to use inputfilter and set it to your editText you can see a workaround to do that here :

How do I use InputFilter to limit characters in an EditText in Android?

also i found a code snippet to this like below:

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++) {
        if (!isEnglishOrHebrew(source.charAt(i))) { 
            return "";
        }
    }
    return null; 
}

private boolean isEnglishOrHebrew(char c) {
    . . .
}
}; 

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

Upvotes: 1

Related Questions