How to change systemUIVisibility on SdkVersion 30
Yes now since it is deprecated, you can use:
window.setDecorFitsSystemWindows(false)
Then make sure you make the status bar transparent as well by adding below style to your app theme
<item name="android:navigationBarColor">@android:color/transparent</item>
Hope this helps :)
decorView.systemUiVisibility deprecated. How to replace it correctly?
Try this methode to change status bar with new api or old :
private fun changeSystemStatusBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val window = window
val decorView = window.decorView
val wic = WindowInsetsControllerCompat(window, decorView)
wic.isAppearanceLightStatusBars = false // true or false as desired.
// And then you can set any background color to the status bar.
window.statusBarColor = ContextCompat.getColor(
this,
R.color.black
)
} else {
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; // set status text dark
window.statusBarColor = ContextCompat.getColor(
this,
R.color.black
)
}
setStatusBarLightText(window, true)
return
}
private fun setStatusBarLightText(window: Window, isLight: Boolean) {
setStatusBarLightTextOldApi(window, isLight)
setStatusBarLightTextNewApi(window, isLight)
}
private fun setStatusBarLightTextOldApi(window: Window, isLight: Boolean) {
val decorView = window.decorView
decorView.systemUiVisibility =
if (isLight) {
decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
} else {
decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
private fun setStatusBarLightTextNewApi(window: Window, isLightText: Boolean) {
ViewCompat.getWindowInsetsController(window.decorView)?.apply {
// Light text == dark status bar
isAppearanceLightStatusBars = !isLightText
}
}
Immersive fullscreen on Android 11
For compatibility, use WindowCompat
and WindowInsetsControllerCompat
. You'll need to upgrade your gradle dependency for androidx.core
to at least 1.6.0-alpha03
so that there will be support for setSystemBarsBehavior
on SDK < 30. mainContainer
is the top-level ConstraintLayout
in my activity.
private fun hideSystemUI() {
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, mainContainer).let { controller ->
controller.hide(WindowInsetsCompat.Type.systemBars())
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
private fun showSystemUI() {
WindowCompat.setDecorFitsSystemWindows(window, true)
WindowInsetsControllerCompat(window, mainContainer).show(WindowInsetsCompat.Type.systemBars())
}
You can find out more information about WindowInsets
by watching this YouTube video
EDIT:
I didn't consider display cutouts or in-display cameras in this answer previously. In the app theme style, i added the following to display my content above the cutout (to the top of the screen when in portrait mode):
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
You can read more at this link: Display Cutout
Non-deprecated way to hide status bar on android?
You could use this:
FrameLayout frameLayout = findViewById<FrameLayout>(R.id.mainView)
frameLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
for the new android version, where R.id.mainView is your main layout.
Please check this for more details: https://proandroiddev.com/exploring-windowinsets-on-android-11-a80cf8fe19be
How do I hide the status bar in my Android 11 app?
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. UseWindowInsetsController
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()
);
Related Topics
I Can't Find the Android Keytool
Fragment Transaction Animation: Slide in and Slide Out
How to Implement Draggable Map Like Uber Android, Update with Change Location
Android - Share on Facebook, Twitter, Mail, Ecc
What's the Difference Between Setwebviewclient VS. Setwebchromeclient
How to Make a Smaller Ratingbar
How to Prevent Going Back to the Previous Activity
Android: Test Push Notification Online (Google Cloud Messaging)
How to Start Power Manager of All Android Manufactures to Enable Background and Push Notification
Vertical Line Using Xml Drawable
Programmatically Add Id to R.Id
How to Get Contacts' Phone Number in Android
Android: How to Share Image with Text on Facebook via Intent
How to Keep an Alertdialog Open After Button Onclick Is Fired