Created
September 18, 2020 15:00
-
-
Save milhauscz/dac974df16b492329110140b1a5d324c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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