Как получить высоту клавиатуры, когда ничего не настроить?
Я пытаюсь получить высоту клавиатуры Android с помощью следующего кода.
Решение popupWindow не работает на некоторых устройствах, есть ли другое решение?
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;
}
}
});
1 ответ
Я бы пошел с insets
решение - где rootWindowInsets.systemWindowInsetBottom
будет включать клавиатуру, если присутствует. Посмотрите на документацию
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;
}
});
Полный код по этой ссылке. эта ссылка также помогает для AdjustNothing.