Search code examples
androidandroid-6.0-marshmallowandroid-themeandroid-8.0-oreo

Issue with toggling status bar color on Android O


Something is not working; I don’t know if it's a problem with the emulator or it's a bug on Android Oreo (I have not a physical device with Android O), but I can’t switch from dark to light status bar theme like in Marshmallow.

styles.xml (from api 23):

<resources>

    <style name="ThemeLight" parent="MyBaseTheme">
        <item name="android:windowBackground">?attr/colorPrimary</item>
        <item name="android:statusBarColor">?attr/colorPrimary</item>
        <item name="android:windowLightStatusBar">true</item>
    </style>


    <style name="ThemeDark" parent="MyBaseThemeDark">
        <item name="android:windowBackground">?attr/colorPrimary</item>
        <item name="android:statusBarColor">?attr/colorPrimary</item>
        <item name="android:windowLightStatusBar">false</item>
    </style>


</resources>

This works fine on Android M, when I change the theme with setTheme(), but in Android O, once I switch to the light theme, it is no longer changed and the status bar remains dark (windowLightStatusBar = true) /:


Solution

  • It must be a bug and I have reported it to google (https://issuetracker.google.com/issues/65883460).

    I use these code to temporary solve the problem.

    // fix windowLightStatusBar not changed after applyStyle on Android O
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        final TypedArray a = obtainStyledAttributes(new int[]{android.R.attr.windowLightStatusBar});
        final boolean windowLightStatusBar = a.getBoolean(0, false);
        a.recycle();
        int flag = getWindow().getDecorView().getSystemUiVisibility();
        if (windowLightStatusBar) {
            flag |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        } else {
            flag &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        }
        getWindow().getDecorView().setSystemUiVisibility(flag);
    }