Show Navigation Bar on Android KitKat after hiding it

12,843

Solution 1

Use this flags for showing it again

    SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN  |
 SYSTEM_UI_FLAG_LAYOUT_STABLE

Solution 2

A solution of mine.

private void hideNavigationBar() {
    // set navigation bar status, remember to disable "setNavigationBarTintEnabled"
    final int flags = 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_IMMERSIVE_STICKY;
    // This work only for android 4.4+
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().getDecorView().setSystemUiVisibility(flags);

        // Code below is to handle presses of Volume up or Volume down.
        // Without this, after pressing volume buttons, the navigation bar will
        // show up and won't hide
        final View decorView = getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    decorView.setSystemUiVisibility(flags);
                }
            }
        });
    }
}

private void showNavigationBar() {
    // set navigation bar status, remember to disable "setNavigationBarTintEnabled"
    final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    // This work only for android 4.4+
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().getDecorView().setSystemUiVisibility(flags);

        // Code below is to handle presses of Volume up or Volume down.
        // Without this, after pressing volume buttons, the navigation bar will
        // show up and won't hide
        final View decorView = getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    decorView.setSystemUiVisibility(flags);
                }
            }
        });
    }
}

The difference is removing "View.SYSTEM_UI_FLAG_HIDE_NAVIGATION".

Solution 3

This will help to show navigation bar:-

private void showSystemUI() {

mDecorView.setSystemUiVisibility(
 View.SYSTEM_UI_FLAG_LAYOUT_STABLE
 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

}

mDecorView is your parent layout. Refer to this link for more details https://developer.android.com/training/system-ui/immersive.html

Solution 4

Full full screen mode

activity.getWindow()
        .getDecorView()
        .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);

Restore

activity.getWindow()
        .getDecorView()
        .setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

Solution 5

The best solution is used in all my projects

public final class SystemUIUtil {

public static void hideDefaultControls(@NonNull final Activity activity) {
    final Window window = activity.getWindow();

    if (window == null) {
        return;
    }

    window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    final View decorView = window.getDecorView();

    if (decorView != null) {
        int uiOptions = decorView.getSystemUiVisibility();

        if (Build.VERSION.SDK_INT >= 14) {
            uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }

        if (Build.VERSION.SDK_INT >= 16) {
            uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }

        if (Build.VERSION.SDK_INT >= 19) {
            uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }

        decorView.setSystemUiVisibility(uiOptions);
    }
}

public static void showDefaultControls(@NonNull final Activity activity) {
    final Window window = activity.getWindow();

    if (window == null) {
        return;
    }

    window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    final View decorView = window.getDecorView();

    if (decorView != null) {
        int uiOptions = decorView.getSystemUiVisibility();

        if (Build.VERSION.SDK_INT >= 14) {
            uiOptions &= ~View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }

        if (Build.VERSION.SDK_INT >= 16) {
            uiOptions &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }

        if (Build.VERSION.SDK_INT >= 19) {
            uiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }

        decorView.setSystemUiVisibility(uiOptions);
    }
}

}

Share:
12,843
erinys
Author by

erinys

Updated on June 09, 2022

Comments

  • erinys
    erinys almost 2 years

    In my app I want to let the user hide or show the Navigation Bar (back, home, menu). The System Bar (battery, wifi, notifications) should always be hidden.

    I hide the System Bar with this code:

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                requestWindowFeature(Window.FEATURE_NO_TITLE);     
    

    And I am successfully hiding the Navigation Bar with these flags:

    final static int flagsHide = 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;
    

    But I haven't found on how to show the Navigation Bar again. Can I do that with flags or do I need something else? The System Bar has to stay invisible.