How to hide navigation bar in Android app?
Asked Answered
V

8

12

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.

Vexillum answered 12/6, 2015 at 21:43 Comment(3)
Do you want to hide the navigation bar in the IDE or in your app?Portcullis
In the app but Ganga Naidu answered it. Thanks anywaysVexillum
Does this answer your question? How to hide navigation bar permanently in android activity?Babul
F
23

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);
}
Flavouring answered 12/6, 2015 at 21:45 Comment(5)
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 reputationVexillum
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?Marillin
Instead of just IMMERSIVE you use IMMERSIVE_STICKY if you want the navbar to auto hide like the notification bar does.Counterstamp
I find on certain devices it causes the first touch in the app to be ignored.Hong
Navigation bar overlap footer layout after called the method showSystemUIPolymerism
U
17

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

Unshaped answered 27/10, 2017 at 16:19 Comment(0)
S
11

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);
    }
}
Sivas answered 18/11, 2019 at 11:51 Comment(2)
Android Studio does not understand this code, View decorView flagged red.Rawley
@MarkKortink - View decorView is just an object of View class.Sivas
B
5

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);
Boscage answered 1/5, 2018 at 22:33 Comment(1)
There is one problem: turn screen off and on and you will see the navigation bar again.Halftone
I
0

In your activity's onCreate method:

this.getWindow()
    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
              WindowManager.LayoutParams.FLAG_FULLSCREEN);
Industrialist answered 12/6, 2015 at 21:47 Comment(2)
That only hides it until the user taps the screenVexillum
It doesn't in my app. Does you app have some onClickListener that might change your window flags?Industrialist
K
0

You Can Create An Activity With The Snippet Above,And Then Add Another Windows Above The Layout:

LayoutParams layoutParameteres=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        v.setLayoutParams(layoutParameteres);
        final WindowManager.LayoutParams parameters=new WindowManager.LayoutParams(sizeX, sizeY, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        parameters.gravity=Gravity.CENTER;
        parameters.x=0;
        parameters.y=0;
        wm.addView(v, parameters);

Good Luck With It

Koweit answered 19/10, 2016 at 11:15 Comment(0)
P
0

setSystemUiVisibility is deprecated. Deprecated in Java

you can also use like this:

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
  
    val windowInsetsController =
        WindowCompat.getInsetsController(window, window.decorView)
    // Configure the behavior of the hidden system bars.
    windowInsetsController.systemBarsBehavior =
        WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE

    // Add a listener to update the behavior of the toggle fullscreen button when
    // the system bars are hidden or revealed.
    window.decorView.setOnApplyWindowInsetsListener { view, windowInsets ->
        // You can hide the caption bar even when the other system bars are visible.
        // To account for this, explicitly check the visibility of navigationBars()
        // and statusBars() rather than checking the visibility of systemBars().
        if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars())
            || windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) {
            binding.toggleFullscreenButton.setOnClickListener {
                // Hide both the status bar and the navigation bar.
                windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
            }
        } else {
            binding.toggleFullscreenButton.setOnClickListener {
                // Show both the status bar and the navigation bar.
                windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
            }
        }
        view.onApplyWindowInsets(windowInsets)
    }
}

JAVA

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    WindowInsetsControllerCompat windowInsetsController =
            WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
    // Configure the behavior of the hidden system bars.
    windowInsetsController.setSystemBarsBehavior(
            WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    );

    // Add a listener to update the behavior of the toggle fullscreen button when
    // the system bars are hidden or revealed.
    getWindow().getDecorView().setOnApplyWindowInsetsListener((view, windowInsets) -> {
        // You can hide the caption bar even when the other system bars are visible.
        // To account for this, explicitly check the visibility of navigationBars()
        // and statusBars() rather than checking the visibility of systemBars().
        if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars())
                || windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) {
            binding.toggleFullscreenButton.setOnClickListener(v -> {
                // Hide both the status bar and the navigation bar.
                windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
            });
        } else {
            binding.toggleFullscreenButton.setOnClickListener(v -> {
                // Show both the status bar and the navigation bar.
                windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
            });
        }
        return view.onApplyWindowInsets(windowInsets);
    });
}

Referances : https://developer.android.com/develop/ui/views/layout/immersive

https://memreaktas.blogspot.com/2024/03/how-to-hide-navigation-a-bar-in-Android-Kotlin.html

Psychoneurosis answered 16/3 at 9:21 Comment(0)
P
-2

Modify this line in the AndroidManifest.xml file

android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"
Porcia answered 4/4, 2021 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.