jack
jack

Reputation: 41

How to get keyboard height when adjust nothing?

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;
                    }

                }
            });

Upvotes: 4

Views: 2154

Answers (2)

Filipkowicz
Filipkowicz

Reputation: 669

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

Upvotes: 1

user6693721
user6693721

Reputation:

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.

Upvotes: -1

Related Questions