4

I am trying to get android keyboard height with following code.

The popupWindow solution doesn't work in some devices , is there another solution?

parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {

                    Rect r = new Rect();
                    parentLayout.getWindowVisibleDisplayFrame(r);

                    int screenHeight = parentLayout.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom);

                    previousHeightDiffrence = heightDifference;

                       if (heightDifference > 100) {
                        isKeyBoardVisible = true;
                        changeKeyboardHeight(heightDifference);

                    } else {

                       if(emojiKeyboard.getVisibility()==View.INVISIBLE){
                          emojiKeyboard.setVisibility(View.GONE);
                        }

                       isKeyBoardVisible = false;
                    }

                }
            });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
jack
  • 41
  • 2

2 Answers2

1

I would go with insets solution - where rootWindowInsets.systemWindowInsetBottom would include keyboard if present. Take a look at documentation

Filipkowicz
  • 639
  • 6
  • 17
  • 1
    Can you give me a liitle bit more info? I've added ViewCompat.setOnApplyWindowInsetsListener(rootView), but this callback called just once when Activity created and doesn't do anything when I open/close keyboard. – Den May 09 '19 at 05:53
  • you can always call `View.requestApplyInsets()` to force new call – Filipkowicz May 10 '19 at 12:55
  • 1
    Yeah, but how do I know when should I call it if I don't get any callback when keyboard is opened/closed? – Den May 13 '19 at 10:45
-1
final Window mRootWindow = getWindow();
        ll_main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
            public void onGlobalLayout(){
                int screenHeight = ll_main.getRootView().getHeight();
                Rect r = new Rect();
                View view = mRootWindow.getDecorView();
                view.getWindowVisibleDisplayFrame(r);

                int keyBoardHeight = screenHeight - r.bottom;
            }
        });

Full code on this link. this link also help for AdjustNothing.