[Transfer] Android implements the hiding and display function of the system status bar
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:
- Change the
themeinAndroidManifest.xmlto:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
- Run the following code before
setContentView:
requestWindowFeature(Window.FEATURE_NO_TITLE)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
Use the
setSystemUiVisibilitymethod ofView. -
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:
- View.SYSTEM_UI_FLAG_VISIBLE: shows the status bar; the Activity is not full screen (back to the normal state).
- View.INVISIBLE: hides the status bar, and the Activity stretches to full screen.
- View.SYSTEM_UI_FLAG_FULLSCREEN: the Activity is full screen and the status bar is hidden.
- 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.
- View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION: same effect as View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN.
- View.SYSTEM_UI_LAYOUT_FLAGS: same effect as View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN.
- View.SYSTEM_UI_FLAG_HIDE_NAVIGATION: hides the virtual buttons (navigation bar). Some phones use virtual buttons instead of physical ones.
- 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)