Android API level 30 setSystemBarsAppearance doesn't overwrite theme data
Asked Answered
G

2

18

Pre-Android 11 (API Level 30) I had <item name="android:windowLightStatusBar">true</item> set in my theme and was additionally changing this (when needed) in the the code with

fun setLightStatusBar(){ 
    window?.decorView?.let { it.systemUiVisibility = it.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR }
} 

fun setDarkStatusBar(){
    window?.decorView?.let { it.systemUiVisibility = it.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() }
}

However, Android-30 adds a new way to control with

fun setLightStatusBar(){ 
    window?.insetsController?.setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS)
} 

fun setDarkStatusBar(){
    window?.insetsController?.setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS)
}

but my issue is that this cannot overwrite the theme-set values and thus I need to either do it all with styles or all in code.

My question is if this is intended to be like this or am I missing something somewhere?

Gaylagayle answered 22/10, 2020 at 12:5 Comment(3)
Crazy droid. Also does not work, I just use the deprecated API and everything works (no other choice) ¯_(ツ)_/¯Crossgarnet
Just checked and it works for api 31 but not for api 30Craniometer
Typical android huh, instead of giving us an abstraction library which handles system bar color in all API version, we were made to manually write if () else {} condition on each API version. :-DValedictorian
W
3

I removed <item name="android:windowLightStatusBar">true/false</item> from styles.xml and it worked correctly.

Whiskey answered 5/12, 2020 at 17:44 Comment(1)
Does not work for me, the flag is not cleared to dark status barBefit
T
3

If you set android:windowLightStatusBar to true in your theme, you need to do the deprecated call, which removes the system UI flag, to make it work.

activity.window.decorView.run {
    systemUiVisibility =
        systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
}

It seems like both WindowInsetsController and systemUiVisibility both controls the status bar theme, but in different mechanisms.

Tricostate answered 26/8, 2021 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.