Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide navigation bar permanently in android activity?

I want to hide navigation bar permanently in my activity(not whole system ui). now i'm using this piece of code

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

It hides the bar but when user touches the screen it showing again. is there any way to hide it permanently until activity onStop();

like image 367
Sujith Manjavana Avatar asked Feb 12 '14 09:02

Sujith Manjavana


People also ask

How do I permanently hide my navigation bar?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.


1 Answers

Snippets:

FullScreenFragment.java

HideNavigationBarComponent.java


This is for Android 4.4+

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

Fast snippet (for an Activity class):

private int currentApiVersion;  @Override @SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      currentApiVersion = android.os.Build.VERSION.SDK_INT;      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_FULLSCREEN         | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;      // This work only for android 4.4+     if(currentApiVersion >= 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);                     }                 }             });     }  }   @SuppressLint("NewApi") @Override public void onWindowFocusChanged(boolean hasFocus) {     super.onWindowFocusChanged(hasFocus);     if(currentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)     {         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);     } } 

If you have problems when you press Volume up or Volume down that your navigation bar show. I added code in onCreate see setOnSystemUiVisibilityChangeListener

Here is another related question:

Immersive mode navigation becomes sticky after volume press or minimise-restore

like image 160
Dawid Avatar answered Oct 07 '22 02:10

Dawid