How do I Detect if Software Keyboard is Visible on Android Device?


Is there a way in Android to detect if the software (a.k.a. "soft") keyboard is visible on screen?

11/3/2014 4:54:51 PM

Accepted Answer

There is no direct way - see where Dianne Hackborn from the Android team has replied. However, you can detect it indirectly by checking if the window size changed in #onMeasure. See How to check visibility of software keyboard in Android?.

5/23/2017 11:54:44 AM

This works for me. Maybe this is always the best way for all versions.

It would be effective to make a property of keyboard visibility and observe this changes delayed because the onGlobalLayout method calls many times. Also it is good to check the device rotation and windowSoftInputMode is not adjustNothing.

boolean isKeyboardShowing = false;
void onKeyboardVisibilityChanged(boolean opened) {
    print("keyboard " + opened);

// ContentView is the root view of the layout of this activity/fragment    
    new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {

        Rect r = new Rect();
        int screenHeight = contentView.getRootView().getHeight();

        // r.bottom is the position above soft keypad or device button.
        // if keypad is shown, the r.bottom is smaller than that before.
        int keypadHeight = screenHeight - r.bottom;

        Log.d(TAG, "keypadHeight = " + keypadHeight);

        if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
            // keyboard is opened
            if (!isKeyboardShowing) {
                isKeyboardShowing = true
        else {
            // keyboard is closed
            if (isKeyboardShowing) {
                isKeyboardShowing = false

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow