Skip to content Skip to sidebar Skip to footer

Prevent Soft Keyboard From Being Dismissed

There are many questions related to how to programatically show/hide the soft keyboard. However, as we all know the android back button will cause the keyboard to be dismissed. Is

Solution 1:

I've found solution:

publicclassKeyBoardHolderextendsEditText {
    publicKeyBoardHolder(Context context) {
        super(context);
    }

    publicKeyBoardHolder(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    publicKeyBoardHolder(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)publicKeyBoardHolder(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @OverridepublicbooleanonKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            returntrue;
        }
        returnfalse;
    }
}

This prevents keyboard from being closed by back button.

Solution 2:

I did it by using following two methods:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK)     
    {
        ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput
                (InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
    return super.onKeyUp(keyCode, event);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK)     
    {
        ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput
                (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
    return super.onKeyDown(keyCode, event);
}

Post a Comment for "Prevent Soft Keyboard From Being Dismissed"