How to hide navigation bar in Android app?

41,872

Solution 1

use immersive mode check this Immersive mode

    // 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.
    mDecorView.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() {
    mDecorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

Solution 2

Here it is in the context of the onCreate method:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.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);

    }

The Android docs have a good explanation of what the different flags do: Using Immersive Full-Screen Mode

Solution 3

Just put this method in your activity where you want to hide status bar and navigation bar in sticky mode.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    //This is used to hide/show 'Status Bar' & 'System Bar'. Swip bar to get it as visible.
    View decorView = getWindow().getDecorView();
    if (hasFocus) {
        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);
    }
}

Solution 4

the easy way is adding this 2 lines after super.onCreate(savedInstanceState);

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Share:
41,872
random user
Author by

random user

Updated on July 25, 2022

Comments

  • random user
    random user almost 2 years

    I was wondering how I could hide the navigation bar in an Android application?

    I know how to hide it initially, but as soon as I touch the screen it pops back up. I want to hide it the same way games like Clash of Clans hide it where the only way to make it pop up is by swiping down the notifications or by swiping where the navigation bar should be.

  • random user
    random user almost 9 years
    That only hides it until the user taps the screen
  • random user
    random user almost 9 years
    This is exactly what I was looking for! Thanks!
  • random user
    random user almost 9 years
    I have to wait another 2 minutes to accept as an answer and I can't click the up arrow since I don't have enough reputation
  • John D.
    John D. almost 9 years
    It doesn't in my app. Does you app have some onClickListener that might change your window flags?
  • hegerber
    hegerber almost 5 years
    Thanks so much, it helped hide the bar. However, once I swipe up to reveal the navbar, it doesn't hide itself after awhile like the status bar does. How can I get the navbar to hide itself after the user reinteracts with the game?
  • Colby Ray
    Colby Ray over 4 years
    Instead of just IMMERSIVE you use IMMERSIVE_STICKY if you want the navbar to auto hide like the notification bar does.
  • Clinton
    Clinton about 4 years
    I find on certain devices it causes the first touch in the app to be ignored.
  • Ram
    Ram about 4 years
    Navigation bar overlap footer layout after called the method showSystemUI
  • Mark Kortink
    Mark Kortink over 3 years
    Android Studio does not understand this code, View decorView flagged red.
  • Chirag Prajapati
    Chirag Prajapati over 3 years
    @MarkKortink - View decorView is just an object of View class.