Soft keyboard open and close listener in an activity in Android

N Sharma picture N Sharma · Aug 9, 2014 · Viewed 170k times · Source

I have an Activity where there are 5 EditTexts. When the user clicks on the first EditText, the soft keyboard opens to enter some value in it. I want to set some other View's visibility to Gone when the soft keyboard opens and also when the user clicks on the first EditText and also when the soft keyboard closes from the same EditText on the back button press. Then I want to set some other View's visibility to visible.

Is there any listener or callback or any hack for when the soft keyboard opens from a click on the first EditText in Android?

Answer

Gal Rom picture Gal Rom · Jul 26, 2016

Piece of cake with the awesome KeyboardVisibilityEvent library

KeyboardVisibilityEvent.setEventListener(
    getActivity(),
    new KeyboardVisibilityEventListener() {
        @Override
        public void onVisibilityChanged(boolean isOpen) {
            // Ah... at last. do your thing :)
        }
    });

Credits for Yasuhiro SHIMIZU