androidandroid-layoutkeyboard

How to make Android Soft keyboard showing or hiding listener better


I have seen a lot of questions about the solution for the soft keyboard show/hide listener.

I think this solution is great.But actually it is not working.So bad. Listen for keyboard show or hide event in android

Finally,I used this solution.But I think this solution is Just a temporary solution.We don't know 200dp represents soft keyboard correctly. How to check visibility of software keyboard in Android?

I want EditText and Textview which is below EditText both are on the top of soft keyboard, when soft keyboard is showing. And I hope EditText and TextView(The Parent layout is LinearLayout) are not always align parent bottom ,so ... android:windowSoftInputMode="adjustresize" is not appropriate.

I think adjustPan is great but it only let EditText on the top when EditText is on focus.I want both them on top.T_T Help me! Thanks a lot.


Solution

  • To know if the keyboard is hidden or visible, I use this code:

     view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    view.getWindowVisibleDisplayFrame(r);
                    if (view.getRootView().getHeight() - (r.bottom - r.top) > 500) { // if more than 100 pixels, its probably a keyboard...
                        onKeyboardShow();
                    } else {
                        onKeyboardHidden();
                    }
                }
            });
    

    onKeyboardShow & onKeyboardHidden functions are my own which then do what's needed.