Skip to content

Instantly share code, notes, and snippets.

@sonique6784
Created May 3, 2023 20:52
Show Gist options
  • Save sonique6784/c6dd6b5fe1923206ff3b5292584ca93b to your computer and use it in GitHub Desktop.
Save sonique6784/c6dd6b5fe1923206ff3b5292584ca93b to your computer and use it in GitHub Desktop.
/**
* immersiveExperience for Android
* set your app into immersive mode hidding navigation and status bar
* call this function in Activity#onCreate
*/
private fun immersiveExperience() {
val windowInsetsController =
WindowCompat.getInsetsController(window, window.decorView)
// Configure the behavior of the hidden system bars.
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// Add a listener to update the behavior of the toggle fullscreen button when
// the system bars are hidden or revealed.
window.decorView.setOnApplyWindowInsetsListener { view, windowInsets ->
// You can hide the caption bar even when the other system bars are visible.
// To account for this, explicitly check the visibility of navigationBars()
// and statusBars() rather than checking the visibility of systemBars().
if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars())
|| windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())
) {
// Hide both the status bar and the navigation bar.
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}
view.onApplyWindowInsets(windowInsets)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment