3

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.

Community
  • 1
  • 1
racwen
  • 33
  • 1
  • 3

1 Answers1

7

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.

apmartin1991
  • 3,064
  • 1
  • 23
  • 44
  • Thanks~But I don't want this solution.I have said "But I think this solution is Just a temporary solution.We don't know 500dp(or pixels) represents soft keyboard correctly". – racwen Jan 06 '17 at 16:30
  • 1
    There is no temporary solution. I was actually looking around today and whilst there are libraries which are more of helper classes. There is nothing permanent as Google don't have classes for this. Googles support for the keyboard is awful and I wish they would fix this and give us official support for the Keyboard where developers can EASILY to this kind of stuff (including hiding and showing the keyboard) – apmartin1991 Jan 06 '17 at 16:35
  • Thank you for this detailed information. I hope the official will be released API. x_x – racwen Jan 07 '17 at 14:15