Skip to content

Instantly share code, notes, and snippets.

@milhauscz
Created September 18, 2020 15:00
Show Gist options
  • Save milhauscz/dac974df16b492329110140b1a5d324c to your computer and use it in GitHub Desktop.
Save milhauscz/dac974df16b492329110140b1a5d324c to your computer and use it in GitHub Desktop.
/**
* Registers a listener which is informed when UI is shown (true) or hidden (false).
*/
fun Window.addSystemUIVisibilityListener(visibilityListener: (Boolean) -> Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
decorView.setOnApplyWindowInsetsListener { v, insets ->
val suppliedInsets = v.onApplyWindowInsets(insets)
// only check for statusBars() and navigationBars(), because captionBar() is not always
// available and isVisible() could return false, although showSystemUI() had been called:
visibilityListener(suppliedInsets.isVisible(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()))
suppliedInsets
}
} else {
@Suppress("DEPRECATION")
decorView.setOnSystemUiVisibilityChangeListener {
visibilityListener((it and View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment