Night colors are not applied when theme changes

643 Views Asked by At

A couple of months ago I implemented night mode in my Android app. The most important changes were that my theme inherits from Theme.AppCompat.DayNight.NoActionBar and I created a values-night folder with a colors.xml which defined the same colors as values/colors.xml just with different values.

This basically worked fine. The only problem is, that when the theme changed, the app (MainActivity) restarted. I could circumvent this by adding uiMode to the android:configChanges. Now the app is not restarted anymore, but some colors are not changed. Notably those of the statusbar and toolbar.

My values/style.xml contains this:

<style name="AppTheme.Base" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="colorControlHighlight">@color/primaryDark</item>
    <item name="colorControlActivated">@color/primaryDark</item>
    <item name="android:windowBackground">@color/window_background</item>
    <item name="android:statusBarColor">@color/statusBar</item>
</style>
<style name="AppTheme.Toolbar" parent="AppTheme">
    <item name="android:drawableTint">@color/navBarTextColor</item>
</style>

The toolbar is:

<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/navBarBackground"
    android:theme="@style/AppTheme.Toolbar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

And the colors - values/colors.xml and values-night/colors.xml:

<resources>
  <color name="primary">#EC9522</color>
  <color name="primaryDark">#CE7704</color>
  <color name="accent">#009688</color>
  <color name="window_background">#FFFFFF</color>
  <color name="statusBar">#CE7704</color>
  <color name="navBarTextColor">#FFFFFF</color>
  <color name="navBarBackground">#EC9522</color>

The background colors of statusbar and toolbar and the tint color of the toolbar should change - which they do when I restart the app. But if I open my app, then switch to the Android Settings / Display and change the design, then these colors don't change.

I checked that the onConfigurationChanges method is called and the newConfig contains the correct uiMode. I also tried to call setTheme there again, but didn't see any difference.

What am I missing?

This is a Xamarin.Forms app, but I don't think that this matters. There only seem to be problems with the native Android part. The colors which are set from XF work. I use Xamarin.Forms 5.0.0.2012 and Xamarin.AndroidX.AppCompat 1.2.0.7. The device is a Pixel 4 with Android 11.

0

There are 0 best solutions below