Skip to content

Instantly share code, notes, and snippets.

@boring-km
Last active July 4, 2023 08:59
Show Gist options
  • Save boring-km/028d54f2887c40ba5f472b497267efaa to your computer and use it in GitHub Desktop.
Save boring-km/028d54f2887c40ba5f472b497267efaa to your computer and use it in GitHub Desktop.
Android Hide Status Bar and Navigation Bar
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.Window
import android.view.WindowInsets
import android.view.WindowInsetsController
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
fun hideStatusBarAndNavigationBar(window: Window) {
hideUI(window)
hideAgain(window)
}
private fun hideUI(window: Window) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
val controller = window.insetsController
if (controller != null) {
controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
controller.systemBarsBehavior =
WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
} else {
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_IMMERSIVE or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
}
private fun hideAgain(window: Window) {
ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { _, insets ->
val visible = insets.isVisible(WindowInsetsCompat.Type.systemBars())
if (visible) {
// 3초 뒤에 hideStatusBarAndNavigationBar(window) 호출
Handler(Looper.getMainLooper()).postDelayed({
hideUI(window)
}, 3000)
}
insets
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment