Skip to content

Instantly share code, notes, and snippets.

@sonique6784
Created February 27, 2023 22:14
Show Gist options
  • Save sonique6784/91b2806d602769d20754e580260c949e to your computer and use it in GitHub Desktop.
Save sonique6784/91b2806d602769d20754e580260c949e to your computer and use it in GitHub Desktop.
/* Copyright 2023 Cedric Ferry.
SPDX-License-Identifier: Apache-2.0 */
import android.view.Window
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
class FullscreenHelper(val window: Window) {
fun immersiveExperience(behavior: Int = WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE) {
val windowInsetsController =
WindowCompat.getInsetsController(window, window.decorView)
// Configure the behavior of the hidden system bars.
windowInsetsController.systemBarsBehavior = behavior
// Add a listener to update the behavior of the toggle fullscreen button when
// the system bars are hidden or revealed.
ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { 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())
}
ViewCompat.onApplyWindowInsets(view, windowInsets)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment