Monday, March 10, 2014

Android EditText - hide soft keyboard on focus but keep cursor visible

In my current android app I have a lot of input fields (EditText) and I need to disable the soft keyboard on focus, because I use my own kind of input widget.

I've try several solutions, but on some was the cursor not visible or the soft keyboard was blinking on focus for some ms. But this one works:

    public static void disableSoftKeyboard(final EditText v) {
        if (Build.VERSION.SDK_INT >= 11) {
            v.setRawInputType(InputType.TYPE_CLASS_TEXT);
            v.setTextIsSelectable(true);
        } else {
            v.setRawInputType(InputType.TYPE_NULL);
            v.setFocusable(true);
        }
    }