Status Bar appears when soft keyboard appears but not hidden back when soft keyboard disappears while in Immersive Mode
Asked Answered
O

2

16

Initially I set my Activity to be in Immersive Mode with the following code:

View decorView = getWindow().getDecorView();
decorView.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);

Everything works fine until the user clicks on an EditText and the soft keyboard appears. It causes the Status Bar to stick on the top and never hides back again even after the soft keyboard disappears. Strangely enough, I only encounter this problem on LG/Samsung phones, Sony phones do not have any problem with this. Anyone has any idea about this?

Orren answered 4/3, 2016 at 8:25 Comment(3)
did you find any solution?Sylphid
Yes :) apparently I need to add <item name="android:windowFullscreen">true</item> to my activity's xmlOrren
@AndriantoLie Your comment is the correct one. Please add it as an answer and accept it for future readers.Skeie
S
10

Use immersive mode like this.

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        immersiveMode();
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    immersiveMode();
                }
            });
    }

Here is your immersive mode function.

public void immersiveMode() {
        final View decorView = getWindow().getDecorView();
        decorView.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);
              }

Also call immersiveMode() in OnResume(); Now status bar will disappear as your soft key board disappears.

Schatz answered 7/6, 2016 at 6:12 Comment(2)
Basically correct, but a bit of overkill: 1) Just add the listener in your onCreate(), no need to keep adding it; 2) you only need to call the immersiveMode() if visibility >0Pimbley
Thanks for onResume(). I fixed a problem that Toolbar appeared after turning off-on a screen (or you can place in onStart()).Typhogenic
C
1

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.

Chiro answered 11/1, 2018 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.