3

Primary users of my app have 2 languages installed. English and other. Default system language is not English. Users just use language switch button on software keyboard.

One specific EditText field in my app needs to accept only A-Z(capitalization issue is not a problem) and spaces and no other characters (no digits, no non-latin chars,etc).

I understood about solutions with InputFilters like How to create EditText accepts Alphabets only in android? or with TextWatcher-derived but thy only allow app to simple ignore incorrect text and I need to be able to make user not even able to see non-latin1 letters in first place on their on-screen keyboard (I'm aware that it is possible to use hardware keyboard, this is not issue at this time).

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
Doesn't help too (language change button is still visible in keyboard).

I need something like iPhone: Change Keyboard language programmatically but for Android.

Do I have any other option except adding fake keyboard to my app?

iOS have .keyboardType = .asciiCapable and it works in such situations

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Tauri
  • 1,291
  • 1
  • 14
  • 28

2 Answers2

3

You can use input filter and assign it to Edit texts

Refer to example below

public class AlphabetInputFilter implements InputFilter {
Pattern mPattern;

public AlphabetInputFilter() {
    mPattern = Pattern.compile("[a-z]");
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    String formatedSource = dest.subSequence(0, dstart).toString();
    String destPrefix = source.subSequence(start, end).toString();
    String destSuffix = dest.subSequence(dend, dest.length()).toString();
    CharSequence match = TextUtils.concat(formatedSource, destPrefix, destSuffix);
    Matcher matcher = mPattern.matcher(match);
    if (!matcher.matches())
        return "";
    return null;
}

}

And you can assign it edit text

mEdittext.setFilters(new InputFilter[]{
            new AlphabetInputFilter()});

user will not be able to enter any value other than a to z

Tushar Saha
  • 1,978
  • 24
  • 29
  • As noted in question (I will clarify question) - InputFilter approach was tried too. It prevents user from entering text. It doesn't prevent from being able to switch keyboard to other languages – Tauri Oct 05 '18 at 12:25
2

I found at least semi-working solution based on https://stackoverflow.com/a/49710730/1063214

imeOptions="flagForceAscii"  

on EditText.

Now at least non-english keyboard is not shown (and digits,etc are filtered anyway).

Tauri
  • 1,291
  • 1
  • 14
  • 28