Every method I came across to hide status bar of my Android app is deprecated in Android 11.
Does anyone know about any current acceptable method?
Also I use Kotlin to develop my apps.
Every method I came across to hide status bar of my Android app is deprecated in Android 11.
Does anyone know about any current acceptable method?
Also I use Kotlin to develop my apps.
When your device is API-30 (Android 11; minSdkVersion 30) or later , setSystemUiVisibility
is deprecated and you can use the newly introduced WindowInsetsController
instead. (And note that you cannot use WindowInsetsController
on API-29 or earlier).
So the official reference says:
This method was deprecated in API level 30. SystemUiVisibility flags are deprecated. Use
WindowInsetsController
instead.
You should use WindowInsetsController
class.
in Kotlin:
window.decorView.windowInsetsController!!.hide(
android.view.WindowInsets.Type.statusBars()
)
in Java:
getWindow().getDecorView().getWindowInsetsController().hide(
android.view.WindowInsets.Type.statusBars()
);
If you also want to hide NavigationBar:
in Kotlin:
window.decorView.windowInsetsController!!.hide(
android.view.WindowInsets.Type.statusBars()
or android.view.WindowInsets.Type.navigationBars()
)
in Java:
getWindow().getDecorView().getWindowInsetsController().hide(
android.view.WindowInsets.Type.statusBars()
| android.view.WindowInsets.Type.navigationBars()
);
setSystemUiVisibility
is not deprecated and you can use it only. While, only when you use minSdkVersion 30 or later, setSystemUiVisibility
is deprecated and you can use windowInsetsController
. –
Rita API LEVEL < 16
If you want to hide the status bar
in your application you can simply do this by making your app FULLSCREEN. Inside your onCreate
method just add FLAG_FULLSCREEN
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_loading);
This is if Build.VERSION.SDK_INT < 16
.
API LEVEL >= 16 AND < 30
This is for Build.VERSION.SDK_INT
greater than 16;
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
Just add this inside your onCreate
where you want to hide the status bar
. More you can read here: https://developer.android.com/training/system-ui/status#41
EDIT: API LEVEL >= 30
It seems that SYSTEM_UI_FLAG_FULLSCREEN
is also depricated from Android 11 even if it didn't say anything in the documentation. But based on this tutorial you can do this in Android 11 you need to use WindowInsetsController and its hide() method. Like the other answer suggested you can use:
getWindow().getDecorView().getWindowInsetsController().hide(
android.view.WindowInsets.Type.statusBars()
);
So, this is for Android 11 and later, other methods are for earlier versions.
This is the easiest way to hide/show status bar in kotlin.
private fun Activity.hideStatusBars() {
WindowInsetsControllerCompat(window, window.decorView)
.hide(WindowInsetsCompat.Type.statusBars())
}
private fun Activity.showStatusBars() {
WindowInsetsControllerCompat(window, window.decorView)
.hide(WindowInsetsCompat.Type.statusBars())
}
If you need to show/hide both status & navigation bar use systemBars()
instead of statusBars()
as following example.
private fun Activity.hideSystemBars() {
WindowInsetsControllerCompat(window, window.decorView)
.hide(WindowInsetsCompat.Type.systemBars())
}
private fun Activity.showSystemBars() {
WindowInsetsControllerCompat(window, window.decorView)
.hide(WindowInsetsCompat.Type.systemBars())
}
Keep it simple. If SDK is equal to or greater than 30,
private void hideSystemBars() {
View decorView = getWindow().getDecorView();
WindowInsetsController windowInsetsController = decorView.getWindowInsetsController();
if (windowInsetsController == null) {
return;
}
// Configure the behavior of the hidden system bars
windowInsetsController.setSystemBarsBehavior(
WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
);
// Hide both the status bar and the navigation bar
windowInsetsController.hide(android.view.WindowInsets.Type.systemBars());
}
Are you looking for this?
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
© 2022 - 2024 — McMap. All rights reserved.