18

I know how to force numbers, texts etc.., but is there a flag or IME options for me to force the EditText to accept only certain language like English in case my DB fields can accept only English characters? of course I can check and notify the users on bad input but that's not exactly user friendly....

Implementing my own filter on the EditText might also be possible but I'm not sure it will force the keyboard layout itself to be in the language I need.

Any idea?

ouflak
  • 2,458
  • 10
  • 44
  • 49
codeScriber
  • 4,582
  • 7
  • 38
  • 62

5 Answers5

17

You can use flagForceAscii or EditorInfo.IME_FLAG_FORCE_ASCII. The flag is on the IME options. So in XML you can put this attribute in the EditText, imeOptions="flagForceAscii"

This just affects the initial state of the keyboard, of course. https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_FLAG_FORCE_ASCII

androidguy
  • 3,005
  • 2
  • 27
  • 38
16

For English Character only:

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"

With symbols that's, you have defines in digits string:

android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ !@#$%^*?"
L_J
  • 2,351
  • 10
  • 23
  • 28
Surender Kumar
  • 1,152
  • 16
  • 17
2

The possible hack to this could be using EditText's inputType like textVisiblePassword. This forces the keyboard to support only English Characters and Numbers , but on the downside it also disabled mic on the keyboard.

Anyways this could work for your requirement.

1

You should probably filter the input anyways. Forcing English keyboard doesn't force user to use only English letters. User can still long press a character or have a non-english hardware keyboard.

You could listen to text change events and reject all characters that are not accepted by your DB at that point.

Juhani
  • 5,076
  • 5
  • 33
  • 35
  • basically when you set inputMethod to some value other than "normal" the EditText will not let you enter any Character which is not of the InputType you allow, if the user will be in German keyboard for example and will try to enter special letters, and i'm filtering he will simple get nothing and that's confusing behavior. I was wondering if there was a way to tell the keyboard to keep it's english layout... – codeScriber Dec 01 '10 at 13:57
  • 1
    well I'll accept this answer till a better one will come along :-) i think it's parietal solution only... – codeScriber Dec 01 '10 at 15:02
0
   EditText define addTextChangedListener
   editReceiver_No.addTextChangedListener(new MyTextWatcher(this, editReceiver_No));

copy the below class MyTextWatcher

    public class MyTextWatcher implements TextWatcher {
    EditText editTextMessage;
    Context context;

    public MyTextWatcher(Context context, EditText editTextMessage) {
        this.editTextMessage = editTextMessage;
        this.context = context;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void afterTextChanged(Editable s) {
        boolean isEnglish = true;
        for (char c : s.toString().toCharArray()) {
            if (Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN) {
                isEnglish = false;
                break;
            }
        }

        if (isEnglish) {
            Log.e("==>>", "ture");
        } else {
            try {
                Utils.showOtherLanguageAddAlertDialog(context, "", "You can only enter english character");
                editTextMessage.getText().delete(editTextMessage.getText().length() - 1, editTextMessage.getText().length());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

above code is checking only English charaters are types or not

Hardip
  • 360
  • 3
  • 9