Android: how can i tell if the soft keyboard is showing or not?

Viewed 22660

Heres the dilemma: I am showing a screen with 3 input fields and 2 buttons inside of a tab(there are 3 tabs total, and they are on the bottom of the screen). the 2 buttons are set to the bottom left and right of the screen, right above the tabs. when i click on an input field, the tabs and buttons are all pushed up on top of the keyboard.

i desire to only push the buttons up, and leave the tabs where they originally are, on the bottom. i am thinking of setting the visibility of the tabs to GONE once i determine that the soft keyboard is showing, and visibility to VISIBLE once the soft keyboard is gone.

is there some kind of listener for the soft keyboard, or maybe the input field? maybe some tricky use of OnFocusChangeListener for the edit text? How can i determine whether the keyboard is visible or not?

6 Answers

Determining if the keyboard is showing is not possible apparently.

You might want to disable it alltogether with the windowSoftInputMode xml tag in your manifest: http://developer.android.com/reference/android/R.attr.html#windowSoftInputMode. Or you can have a look on how to remove focus to hide the keyboard: Hide soft keyboard on activity without any keyboard operations.

Neither will exactly solve your problem. I remember reading a blogpost strongly advising not to use tabs at the bottom, rather than the top of the screen, for UI clarity reasons. I recommend you to follow up on that.

This might help http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html

https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/latin

The keyboard code is here, I grepped through it a little but give up, I don't see any Broadcasts being sent that would be of any use, at least in here. Maybe you can go find lower level code in the repo and find a useful Intent being sent. The first linke might tell you when it becomes visible, but I'm not sure how to tell when it becomes invisible.

Working solution for me -

ViewTreeObserver treeObserver = getViewTreeObserver();
        if (treeObserver != null)
            treeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int pHeight = getResources().getDisplayMetrics().heightPixels;

                Rect visRect = new Rect();
                viewGroup.getWindowVisibleDisplayFrame(visRect);

                boolean keyboardVisible;
                int keyboardHeight= pHeight-currentHeight;
                if (keyboardHeight > (MIN_KEYBOARD_HEIGHT*pHeight))
                {
                    TGVLog.d(debugTag, "keyboardVisible-- "+true);
                    keyboardVisible = true;
                }
                else
                {
                    TGVLog.d(debugTag, "keyboardVisible-- "+false);
                    keyboardVisible = false;
                }
                }
            });

I finally find solution after looking for several class. I decide to use WindowsInset because it not call every seconde like use global layout. There is a video talking about how to use windowsInset. Link

In my last application I had some issues in displaying the soft keyboard, then I have used the following code in my Manifest file:

<activity android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:name=".rate.Calculator"/>

Related