How to switch the status bar?

I am looking for a way to show and hide the status bar using onClickListener , but only showing that it works.

 WindowManager.LayoutParams lp = getWindow().getAttributes(); if (isStatusbarVisible) lp.flags = LayoutParams.FLAG_FULLSCREEN; else lp.flags = LayoutParams.FLAG_FORCE_NOT_FULLSCREEN; getWindow().setAttributes(lp); isStatusbarVisible = !isStatusbarVisible; 

Hiding the status bar with FLAG_FULLSCREEN seems to work only if the flag is set before calling setContentView() .

Is there any other way to hide the status bar?

+2
android statusbar
source share
4 answers

The answer is pretty simple, clearing the FLAG_FULLSCREEN flag is all that is needed:

 if (isStatusBarVisible) getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); else getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
+5
source share

I searched for this solution and finally came up with this implementation to turn on and off the full screen mode:

 private void toggleFullscreen() { WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setAttributes(attrs); } 

It uses beaten XOR logic to switch FLAG_FULLSCREEN .

+1
source share

There is better (this boolean variable is not required) to switch the full-screen method:

 private void toggleFullscreen() { WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setAttributes(attrs); } 

It uses beaten XOR logic to switch FLAG_FULLSCREEN .

0
source share

As we all saw, the Android API is constantly changing. Here is a more relevant answer to the question.

According to this document: https://developer.android.com/tutorial/system-ash/status.html

Hide status bar on Android 4.1 and higher

You can hide the status bar on Android 4.1 (API level 16) and higher using setSystemUiVisibility (). setSystemUiVisibility () sets the UI flags to an individual viewing level; these parameters are aggregated in the level window. Using setSystemUiVisibility () to set UI flags gives you more granular control over system strings than using WindowManager flags. This snippet hides the status bar:

   decorView = getWindow(). getDecorView(); //   . int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); // ,        ,  //   ,     . ActionBar actionBar = getActionBar(); actionBar.hide(); > 

Borrowing groovy XOR from other answers:

  public void toggleFullscreen() {    decorView = getWindow(). GetDecorView();  int uiOptions = decorView.getSystemUiVisibility();  uiOptions ^ = View.SYSTEM_UI_FLAG_FULLSCREEN;  decorView.setSystemUiVisibility(uiOptions); } > 

This works (I use it) and seems to work the same as getWindow(). setAttributes() solution getWindow(). setAttributes() getWindow(). setAttributes() , but this is recommended in the developer docs.

0
source share

All Articles