Monitor the pop-up and collapse of the soft keyboard

 
 
boolean isVisiableForLast = false;
    int keyboardHeight = 0;
    public void addOnSoftKeyBoardVisibleListener(Activity activity) {
        final View decorView = activity.getWindow().getDecorView();
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                decorView.getWindowVisibleDisplayFrame(rect);
                // Calculate the height of the visible screen
                int displayHight = rect.bottom - rect.top;
                //Get the height of the screen as a whole
                int hight = decorView.getHeight();
                //get keyboard height
                keyboardHeight = hight-displayHight;
                boolean visible = (double) displayHight / hight < 0.8;
                if(visible != isVisiableForLast){
//                    listener.onSoftKeyBoardVisible(visible,keyboardHeight );
                    if (visible){//soft keyboard display
                        
                    }else{ //soft keyboard hide
                      
                    }
                }
                isVisiableForLast = visible;
            }
        });
    }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324539967&siteId=291194637