Programmatically enable/disable Immersive Mode

Jason picture Jason · Jul 7, 2016 · Viewed 8.3k times · Source

I'm looking to make my Android app fullscreen, but only showing the android navigation bar on a certain screen (my settings screen). I know that it is dangerous to hide the navigation bar permanently on a screen, but I want to know if this is possible. I have looked into rooting my device and using the Xposed framework.

Is there a way to programmatically disable the navigation bar, or "sticky mode", and re-enable later?

Edit: I've looked at the Android Immersive Mode but it seems like the navigation bar will still show if the user touches the edge. I want to remove any hint of the navigation bar until they go to my settings screen.

Answer

Arpit Ratan picture Arpit Ratan · Jul 7, 2016

Yes it is possible. Use the below code snippet to achieve the desired functionality.

// This snippet hides the system bars.
private void hideSystemUI() {
    // Set the IMMERSIVE flag.
    // Set the content to appear under the system bars so that the content
    // doesn't resize when the system bars hide and show.
   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 // hide nav bar
            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
            | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
    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);
}

For more details refer the the below google documentation :

https://developer.android.com/training/system-ui/immersive.html

EDIT 1 : To hide it permanently may be you could try something like this (Hacky)

decorView.setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {

                            hideSystemUI();


                    }
                });`