Hide part of activity_main.xml if keyboard is open (Android)

后端 未结 1 589
别那么骄傲
别那么骄傲 2020-12-07 05:37

I have the following activity_main.xml shown at bottom. There, a CoordinatorLayout and two LinearLayouts occur. The first LinearLa

相关标签:
1条回答
  • 2020-12-07 05:51

    We had same issue in past so we have put some observable on full layout object try this and let me know

    /*Hide button when keyboard is open*/
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            view.getWindowVisibleDisplayFrame(r);
    
            int heightDiff = view.getRootView().getHeight() - (r.bottom - r.top);
    
            if (heightDiff > 244) { // if more than 100 pixels, its probably a keyboard...
                //ok now we know the keyboard is up...
                buttonLayout.setVisibility(View.GONE);
    
    
            } else {
                //ok now we know the keyboard is down...
                buttonLayout.setVisibility(View.VISIBLE);
    
    
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题