How to hide navigation bar in Android App Code

16k views Asked by At

I added this code to my app ;

View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

But The code forces me to add "SuppressLint NewApi to MainActivity".Also there is an error about "View.SYSTEM_UI_FLAG_FULLSCREEN".Code says "CHANGE TO...." so what I must do I don't know.

Please help me.Thanks a lot.

5

There are 5 answers

5
MikeKeepsOnShine On

The code that you use is for API level 14 or higher, like described in the documentation here

The SuppressLint NewApi to MainActivity happens because probably your target API level (or your minTarget) is less than 14.

And as this post says, you can't hide the navigation bar

EDIT:

Set your android:minSdkVersion="14" There's a restriction: you if are using a device that don't have the traditional hardware keys (back,Home,Menu) you can't hide the navigation bar.

0
Adam Short On

This is how I do it - hides the title and makes it full screen:

// requesting to turn the title OFF
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

I also have this in my AndroidManifest under Application:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

I should note this a minSDK version of 13 and target of 19 (4.4 KitKat)

0
royco On

I am a very novice developer but I discovered that the solution for me was to change the theme and base my theme around any "...NoActionBar" theme. I haven't got my head around how to build themes or modify styles yet, but I thing this will probably will be the most efficient way to just get rid of the Action bar on top. It does NOT get you into fullscreen mode though. That, I believe you have to do programatically.

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:windowNoTitle">true</item>
</style>
<resources/>

Also, if you want to know more about themes, this seems to be a good place for that: https://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/

0
alex On

For API level 19:

In addition to setting the fullscreen flag, I also had to add the following to hide the soft keys:

View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

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

You can also set the sticky immersion as described here: https://developer.android.com/training/system-ui/immersive.html

0
Rahul Raina On

Here is simple code to hide Navigation bar in Activity. It is working.

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);