中文 English

[Transfer] Android implements the hiding and display function of the system status bar

Published: 2021-07-09 · Views --

Problem

Video apps in particular need to hide the system status bar and go full screen after switching to landscape, so videos can use more of the screen. When switching back to portrait, the status bar should appear again. How do you do that?

Methods found online

For example:

  1. Change the theme in AndroidManifest.xml to:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  1. Run the following code before setContentView:
requestWindowFeature(Window.FEATURE_NO_TITLE) 
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  1. Use the setSystemUiVisibility method of View.

  2. Hide and show the status bar with the following code:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // hide the status bar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // show the status bar

My project had these requirements: in the current Activity, switching to landscape must not destroy and re-create the Activity, and it must hide the system status bar and go full screen; switching back to portrait must show the status bar again. I did not need to hide the title bar.

So methods 1 and 2 did not fit. I tried method 3, calling setSystemUiVisibility, which accepts these values:

  1. View.SYSTEM_UI_FLAG_VISIBLE: shows the status bar; the Activity is not full screen (back to the normal state).
  2. View.INVISIBLE: hides the status bar, and the Activity stretches to full screen.
  3. View.SYSTEM_UI_FLAG_FULLSCREEN: the Activity is full screen and the status bar is hidden.
  4. View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN: the Activity is full screen, but the status bar stays visible and covers the top of the Activity layout.
  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: hides the virtual buttons (navigation bar). Some phones use virtual buttons instead of physical ones.
  8. View.SYSTEM_UI_FLAG_LOW_PROFILE: puts the status bar in low profile mode, hiding some of its icons.

What I needed was View.SYSTEM_UI_FLAG_FULLSCREEN, but with it only the status bar’s content disappeared while its space remained. (Test phones: Huawei Honor 8 running EMUI 5.0 based on Android 7.0; Samsung Galaxy S6 running Android 6.0.)

In the end, method 4 met the requirements.

Summary

That is how to hide and show the system status bar on Android. I hope it helps. If you have questions, leave a comment. This post was reposted from ZaLou.Cn.

Reference

https://cloud.tencent.com/developer/article/1741895 (original article has been removed)

Comments

Sign in with GitHub to comment. Chinese and English versions share the same discussion. Discuss on GitHub

Views --