I have 2 functions, which help me to enable and disable full-screen mode in my app. By default, my app is not in full-screen mode. The behavior that I want is when the user opens a fragment, after 5 seconds I enable full-screen mode. When the user touches the screen, I disable the full-screen mode, but after 3 seconds I enable it again. Here's the code, that I wrote (I'm using the legacy version)
private fun enableFullScreen() {
fullScreenModeEnabled = true
// From docs
decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
private fun disableFullScreen() {
if (fullScreenModeEnabled) {
// From docs
decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
fullScreenModeEnabled = false
}
handler.removeCallbacks(enableFullScreenRunnable)
handler.postDelayed(enableFullScreenRunnable, 3000)
}
The enableFullScreenRunnable
is a simple runnable which calls the enableFullScreen()
function after a certain delay. When the user opens the fragment, after 5 seconds I call the enableFullScreen()
function using the handler and the runnable. I have a click listener for the root view, so when the user clicks on the screen, I call disableFullScreen()
.
rootView.setOnClickListener {
disableFullScreen()
}
And lastly, I have some UI elements, e.g. a textView, which needs to be synced with the state of my app. So when the app goes to full-screen mode, I have to hide my textView and vice versa.
decorView.setOnSystemUiVisibilityChangeListener { visibility ->
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
textView.visibility = View.VISIBLE
} else {
textView.visibility = View.INVISIBLE
}
}
The thing is, that all these flags are deprecated for Android 11. And in Android studio, it says to use WindowsInsets instead of these flags. How can I achieve the same functionality using WindowInsets? In the official docs, the code doesn't up to date and still it uses the old flags here