I am changing the decorView
of my window
to basically toggle between View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
being true and false depending upon a scroll position of my view.
Since I am changing the ActionBar and the status Bar at the same time, I would like both changes to at least be perceptibly instantaneous. However I notice there is a 50ms delay between window.decorView.systemUiVisibility = newIntWithToggledLightMode
being executed and the new status bar mode being rendered. I don't want to have my toolbar changes be placed in a postDelay handler just to coordinate their appearance of simultaneity with the status bar change. I thought I could override onSystemUiVisibilityChangeInterface
to be informed when the status bar ui had been updated but that only seems triggered when I make the status bar invisible, not simply changing the appearance mode. Has anybody run into this and come up with a more elegant solution?
If you use setSystemUIOverlayStyle to control your status bar text color, you can easily change your style by calling SystemChrome.setSystemUIOverlayStyle (SystemUiOverlayStyle.light) again. No variable or set state needed. <1> Start with white status bar text color. <2> Tap the button will change it to black.
Status bar text color change according to our FakeAppBar. If the AnnotatedRegion widget isn't placed around the top area, the value won't be used. This makes perfect sense since the widget color won't interfere with the status bar, so it shouldn't affect the status bar style.
FakeAppBar won't have an effect on the status bar style if it doesn't sit at the top part of the screen. You can change style dynamically just like an app bar with variable and setState because internally, AppBar also uses AnnotatedRegion.
Here is fake app bar I created using AnnotatedRegion. It is a simple pink bar with a white status bar text color. Place this AnnotatedRegion widget around the top area, and the system will configure the system styles based on its value. <1> Put it at the very top.
I just investigated trying to replicate your issue, but I did not encountered the latency that you are talking about... In my example, I simulated the changing background event with a button click. Hoping that it will help you, I share my code here:
activity's attribute
private val isLight get () = (window.decorView.systemUiVisibility and SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) > 0
button initialization in activity's onCreate
button.setOnClickListener {
if (isLight) {
setStatusBarColors(
decorView.systemUiVisibility and SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv(),
ContextCompat.getColor(decorView.context, android.R.color.holo_green_dark)
)
} else {
setStatusBarColors(
decorView.systemUiVisibility or SYSTEM_UI_FLAG_LIGHT_STATUS_BAR,
ContextCompat.getColor(decorView.context, android.R.color.holo_red_light)
)
}
}
and the setStatusBarColors() function
private fun setStatusBarColors(flags: Int, @ColorInt bgColor: Int) {
window.decorView.systemUiVisibility = flags
window.statusBarColor = bgColor
toolbar.setBackgroundColor(bgColor)
}
This code lead to the following result (click on the following GIF if it does not run):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With