8

I am applying the theme Theme.AppCompat.DayNight.NoActionBar to my application.

In the light theme, I want the status bar to be white and in the dark theme, I want it to be dark.

I can't get the status bar to be white and the letters to be dark and vice versa.

This is my style:

 <style name="AppTheme.Base" parent="Theme.AppCompat.DayNight.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorControlNormal">@color/primary_dark</item>
        <item name="colorControlHighlight">@color/primary_dark</item>
        <item name="android:textColorPrimary">@color/letter_medium_emphasis</item>
        <item name="android:textColorSecondary">@color/letter_high_emphasis</item>
    </style>

and this is my color file:

<resources>
    <color name="primary">#ffffff</color>
    <color name="primary_dark">#0336ff</color>
    <color name="letter_high_emphasis">#de000000</color>
    <color name="letter_medium_emphasis">#99000000</color>
    <color name="letter_medium_disabled">#61000000</color>
</resources>

I have tried adding the parameter <item name="android:statusBarColor">@color/primary</item>
But I still get the same thing, the status bar is white and the status bar text is also white.

What is the way to show the white status bar with dark letters in the light theme and vice versa in the dark theme?

Adinia
  • 3,722
  • 5
  • 40
  • 58
Sergio76
  • 3,835
  • 16
  • 61
  • 88

1 Answers1

29

You can use isLightTheme attr:

https://developer.android.com/reference/android/R.attr#isLightTheme

Add it to your themes.xml like so:

<item name="android:windowLightStatusBar">?attr/isLightTheme</item>

The value returned by ?attr/isLightTheme is handled by the system. You can also get the currently set value via:

AppCompatDelegate.getDefaultNightMode();

OR

Configuration config = getResources().getConfiguration();
int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;

Together with android:statusBarColor you can control your status bar in both light and dark theme automatically.

Also, I had much better luck using parent="Theme.MaterialComponents.DayNight" for my base theme when using this attr value.

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
CzarMatt
  • 1,773
  • 18
  • 22
  • Just adding a missing nugget of info here, if using the `Theme.MaterialComponents.DayNight` theme you set the light mode colors as the default colors for `colorPrimary` and `colorPrimaryDark` in `/values/colors.xml`, and override these with the dark mode colours in `/values-night/colors.xml`. – Breeno Mar 16 '23 at 19:27