I have updated the project target API version to 30, and now I see that the systemUiVisibility property is deprecated.
The following kotlin code is the one I'm using which is actually equivalent to setSystemUiVisibility method in Java.
playerView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
Please let me know if you have got any stable replacement for this deprecated code. The google's recommendation is to use WindowInsetsController
, but I don't how to do that.
setDecorFitsSystemWindows( @NonNull Window window, boolean decorFitsSystemWindows. ) Sets whether the decor view should fit root-level content views for WindowInsetsCompat .
To hide status bar in Android using Kotlin, we can request that the visibility of the status bar or other screen/window decorations be changed by setting window. decorView. systemUiVisibility with View. SYSTEM_UI_FLAG_FULLSCREEN in the Activity Kotlin file.
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.
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
For devices with notches at the top of the display, you can add the following to your v27 theme.xml
file make the UI appear either side of the notch:
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
You can read more at this link: Display Cutout
TL;DR snippet
Wrapping in if-else structure needed to avoid java.lang.NoSuchMethodError: No virtual method setDecorFitsSystemWindows
exception on older SDK versions.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { window.setDecorFitsSystemWindows(false) } else { window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN }
Links with full information about insets and fullscreen modes in Android 11
https://blog.stylingandroid.com/android11-windowinsets-part1/
https://www.youtube.com/watch?v=acC7SR1EXsI
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With