Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colors of the status bar using the DayNight theme in android

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?

like image 749
Sergio76 Avatar asked Mar 23 '20 17:03

Sergio76


People also ask

Can you change the color of the status bar android?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar.

What is DayNight theme Android?

The DayNight theme gives an app the cool capability of switching color schemes based on the time of day and the device's last known location. Add the following to your styles.xml : <style name="AppTheme" parent="Theme.AppCompat.DayNight"> <!-- Customize your theme here.

What is theme AppCompat DayNight?

AppCompat. DayNight. DayNight theme allows to switch between light(day) and dark(night) themes, based on the time. Keep in mind that it supports API 14+. Any lower and it defaults to the Light theme.

How do I change the color of my notification bar?

How do I change the status bar color which should be same as in navigation bar. As Niels suggests, there is a THEME ATTRIBUTE since v21 called android:statusBarColor . You can simply add it to values-v21/styles. xml .


1 Answers

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.

like image 69
CzarMatt Avatar answered Oct 19 '22 17:10

CzarMatt