Register a callback for keyboard open and close

suggest change

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);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents