Android Keyboard Register a callback for keyboard open and close

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The idea is to measure a layout before and after each change and if there is a significant change you can be somewhat certain that its the softkeyboard.

// A variable to hold the last content layout hight
private int mLastContentHeight = 0;

private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override public void onGlobalLayout() {
        int currentContentHeight = findViewById(Window.ID_ANDROID_CONTENT).getHeight();

        if (mLastContentHeight > currentContentHeight + 100) {
            Timber.d("onGlobalLayout: Keyboard is open");
            mLastContentHeight = currentContentHeight;
        } else if (currentContentHeight > mLastContentHeight + 100) {
            Timber.d("onGlobalLayout: Keyboard is closed");
            mLastContentHeight = currentContentHeight;
        }
    }
};

then in our onCreate set the initial value for mLastContentHeight

mLastContentHeight = findViewById(Window.ID_ANDROID_CONTENT).getHeight();

and add the listener

rootView.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);

don't forget to remove the listener on destroy

rootView.getViewTreeObserver().removeOnGlobalLayoutListener(keyboardLayoutListener);


Got any Android Question?