How To Get Notify When System UI Visibility Changes In Android?

When ever you hide system bars to give fullscreen experience you should also hide or show other part of you app's UI as per your requirements. i.e. play , pause buttons , game controls etc.
In this post we gonna learn how to listen for system bars hide / show event and make any desired changes accordingly.

To listen for system bars hide / show event,register View.OnSystemUiVisibilityChangeListener() to view.This could be any view you used for controlling navigation visibility.

In Following Code Snipped We Have Registered Listener in Activity's onCreate() method.

public fun onCreate(savedInstanceState : Bundle){
super.onCreate(savedInstanceState)
window.decorView.setOnSystemUiVisibilityChangeListener{ systemUiVisibility ->
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if (systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
//set main layout
setContentView(R.layout.main_activity)
}
view raw MainActivity.kt hosted with ❤ by GitHub

It's best precise to sync you UI with system bar visibility changes.You could use this code to hide and show video player controls in sync with status bar hide and show event.

No comments:

Post a Comment