Android Hide Navigation Bar/Stay in Immersive Mode with Soft Keyboard Appearance

后端 未结 6 1380
时光说笑
时光说笑 2020-12-09 03:34

Working on a client\'s app that is using immersive mode to hide the navigation bar and status bar on every activity using the following code:

int currentApiV         


        
6条回答
  •  Happy的楠姐
    2020-12-09 03:50

    Here is my solution for this ; First I checked if soft keyboard is showed up or not:

    getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
    
                    Rect r = new Rect();
                    getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                    int screenHeight = getWindow().getDecorView().getRootView().getHeight();
    
                    int keypadHeight = screenHeight - r.bottom;
    
                    //Log.d(TAG, "keypadHeight = " + keypadHeight);
    
                    if (keypadHeight > screenHeight * 0.15) { 
                         //Keyboard is opened
                         hideNavBar();
                    }
                    else {
                        // keyboard is closed
                    }
                }
            });
    

    And I have a hideNavBar() method to be triggered when soft keyboard is showed up.

    private void hideNavBar() {
        if (Build.VERSION.SDK_INT >= 19) {
            View v = getWindow().getDecorView();
            v.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
    

    This solves the problem of getting navigation bar while there is an Edittext to be typed.

提交回复
热议问题