Disable keyboard on EditText Disable keyboard on EditText android android

Disable keyboard on EditText


Below code is both for API >= 11 and API < 11. Cursor is still available.

/** * Disable soft keyboard from appearing, use in conjunction with android:windowSoftInputMode="stateAlwaysHidden|adjustNothing" * @param editText */public static void disableSoftInputFromAppearing(EditText editText) {    if (Build.VERSION.SDK_INT >= 11) {        editText.setRawInputType(InputType.TYPE_CLASS_TEXT);        editText.setTextIsSelectable(true);    } else {        editText.setRawInputType(InputType.TYPE_NULL);        editText.setFocusable(true);    }}


Here is a website that will give you what you need

As a summary, it provides links to InputMethodManager and View from Android Developers. It will reference to the getWindowToken inside of View and hideSoftInputFromWindow() for InputMethodManager

A better answer is given in the link, hope this helps.

here is an example to consume the onTouch event:

editText_input_field.setOnTouchListener(otl);private OnTouchListener otl = new OnTouchListener() {  public boolean onTouch (View v, MotionEvent event) {        return true; // the listener has consumed the event  }};

Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox is NULL it will be no longer an editor:

MyEditor.setOnTouchListener(new OnTouchListener(){  @Override  public boolean onTouch(View v, MotionEvent event) {    int inType = MyEditor.getInputType(); // backup the input type    MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input    MyEditor.onTouchEvent(event); // call native handler    MyEditor.setInputType(inType); // restore input type    return true; // consume touch even  }});

Hope this points you in the right direction


You can also use setShowSoftInputOnFocus(boolean) directly on API 21+ or through reflection on API 14+:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {    editText.setShowSoftInputOnFocus(false);} else {    try {        final Method method = EditText.class.getMethod(                "setShowSoftInputOnFocus"                , new Class[]{boolean.class});        method.setAccessible(true);        method.invoke(editText, false);    } catch (Exception e) {        // ignore    }}