Android API level 30 setSystemBarsAppearance doesn't overwrite theme data

3.3k views Asked by At

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?

2

There are 2 answers

1
Ярослав Нестеров On

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

0
Hsingchien Cheng On

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.