Android realizes the function of hiding and displaying the status bar of the system

  • 2021-09-16 08:09:56
  • OfStack

Especially for video APP, it is necessary to hide the system status bar and display it in full screen after switching to the horizontal screen, so as to watch the video with a larger picture. When switching back to the vertical screen, the status bar is displayed again. So how to achieve it?

There are many practices circulating on the Internet. For example:

1. Modify theme in the AndroidManifest. xml file to android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”

2. Execute the following code before the setContentView method:


requestWindowFeature(Window.FEATURE_NO_TITLE) 
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

3. setSystemUiVisibility method through View

4. Hide and display the status bar through the following code:


getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // Hide status bar  
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // Show status bar 

In my project is to achieve the following requirements: in the current Activity, after switching to the horizontal screen, Activity cannot be destroyed and re-initialized, and the system status bar is hidden and displayed in full screen; When switching back to the vertical screen, the status bar is displayed again. Also, I don't need to hide the title bar.

Therefore, methods 1 and 2 are not suitable for me. Method 3, which I used, calls the setSystemUiVisibility method, and the parameters passed in by this method can be:

1. View.SYSTEM_UI_FLAG_VISIBLE: Displays status bar, Activity is not displayed in full screen (reverts to normal state).
2. View. INVISIBLE: Hides the status bar while Activity stretches full screen.
3. View.SYSTEM_UI_FLAG_FULLSCREEN: Activity is displayed in full screen, and the status bar is hidden and overwritten.
4. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN: Activity is displayed in full screen, but the status bar will not be hidden and overwritten, the status bar will still be visible, and the top layout part of Activity will be obscured by the status.
5. View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION: Same effect as View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
6. View.SYSTEM_UI_LAYOUT_FLAGS: Same effect as View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
7. View. SYSTEM_UI_FLAG_HIDE_NAVIGATION: Hide virtual keys (navigation bar). Some phones use virtual buttons instead of physical buttons.
8. View.SYSTEM_UI_FLAG_LOW_PROFILE: The status bar display is in a low-energy display state (low profile mode), and one icon display on the status bar will be hidden.

What I need to pass in here is View.SYSTEM_UI_FLAG_FULLSCREEN But when I pass in this parameter, the result is that only the status bar disappears, but the position is still there. (Test mobile phone: Huawei Glory 8 system is EMUI 5.0 based on Android 7.0; 3-star galaxy s6 system is Android 6.0)

Finally, using method 4, the demand is successfully met.

Summarize


Related articles: