xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.63k stars 1.87k forks source link

[Android] Some colors are not changed when the theme changes #14232

Open MichaelRumpler opened 3 years ago

MichaelRumpler commented 3 years ago

I want to keep my app running when the device changes between light and dark mode. So I added ConfigChanges.UiMode to the ConfigurationChanges in my MainActivity.

Now the activity is not restarted anymore, but some colors are not applied. Mainly those which I specified in the files values/colors.xml and values-night/colors.xml. I have different colors in the values-night folder. This works fine when the app is restarted, but when the theme changes while it is already running, then these colors are not applied.

Here are screenshots from the sample app when it starts in light and dark mode: Started Light Started Dark

When I change the theme, then they look like this: Started Light Switched To Dark Started Dark Switched To Light

I see four issues here:

  1. I specified <item name="android:statusBarColor">@color/statusBar</item> in values/styles.xml with different colors in the colors.xml files. But these are not changed when the theme changes.
  2. The SwitchCells do not have a TextColor property. They seem to magically recognize the color when the app starts and render with light/dark text so that they are readable against the background. But when the theme changes, then the text color stays the same and you don't see the text anymore.
  3. Another problem with the SwitchCell is the switch itself. It usually looks like a circle along a horizontal bar. But when the theme changes, then the bar vanishes and you only see a circle.
  4. I also add a FloatingActionButton with its own Android layout file. It uses a android:tint="@color/white". This color is actually black in the dark theme, but the change is not applied.

Issues 1 and 4 are very similar and may not have something to do with Xamarin.Forms at all. But maybe somebody can help me with these too. I already opened a question at StackOverflow, but nobody could help me there (yet).

But 2 and 3 are definitely Forms problems.

Most colors change fine when defined in XF. This all works in iOS like a charm.

Here is the source of the repro project: Repro_ChangeTheme.zip

tuxmainy commented 3 years ago

Hi, same here so I confirm. I am using ConfigChanges.UiMode and style.xml:

<style name="MainTheme" parent="Theme.AppCompat.DayNight.NoActionBar">

Only changes to colors I made (ContentPage is not using any System default?!):

<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
    <Setter Property="BackgroundColor" Value="{AppThemeBinding Dark=Black, Light=White}"/>
</Style>

Starting the App in Light Mode:

Switching from Light Mode to Dark Mode in Settings:

App after switching (please note, that the ContentPage switched it's background color as defined before):

App after restarting (still in Dark Mode):

So it looks like "only" Android defined colors/themes are affected.

regards Daniel

tuxmainy commented 3 years ago

@MichaelRumpler is mentioning kind of a workaround: Removing ConfigChanges.UiModemakes your App automatically restarting on Dark/Light Mode switching. But if the user is currently interacting with the App this might annoy the user as hell ;)

MichaelRumpler commented 3 years ago

@MichaelRumpler is mentioning kind of a workaround: Removing ConfigChanges.UiModemakes your App automatically restarting on Dark/Light Mode switching. But if the user is currently interacting with the App this might annoy the user as hell ;)

Especially when the device switches themes automatically at a specified time.

udde commented 2 years ago

Any news on this? I have the same behaviour

udde commented 2 years ago

@tuxmainy did you get anywhere with this?

MichaelRumpler commented 2 years ago

It appears like Android does not change colors automatically when the theme changes. So you have to do it manually. @jamesmontemagno explained a workaround for the status bar in this video.

I doubt that the SwitchCell in Xamarin.Forms will be fixed considering that MAUI is around the corner and most of the Forms team is already working on that. OTOH @jfversluis is back fixing Forms bugs and he implemented all the theme stuff, so we might get lucky.

udde commented 2 years ago

@MichaelRumpler Thanks for getting back to me on this one!

jfversluis commented 2 years ago

From what I remember implementing this is that Android indeed needs to recreate the activity in order to apply all the colors successfully. So I doubt if we are able to make the colors in your colors.xml work. I can look at the Forms specific stuff though :)

jamesmontemagno commented 2 years ago

I would take a look at https://github.com/jamesmontemagno/MyCoffeeApp/ as an example. Has iOS in there as well.

udde commented 2 years ago

Thanks for the response @jfversluis. I see. then we'll have to live with it... some native elemnts like pickers and such stay in "wrong" styling/theme. @jamesmontemagno Thanks. I like your videos a lot, Good job!!

tuxmainy commented 2 years ago

@tuxmainy did you get anywhere with this?

Nope, just made my app restart on theme change

jamesmontemagno commented 2 years ago

@tuxmainy make sure your configurationchanges are set correct in your main activity https://github.com/jamesmontemagno/MyCoffeeApp/blob/master/MyCoffeeApp/MyCoffeeApp.Android/MainActivity.cs#L20

You must add ConfigChanges.UiMode

leapse commented 1 year ago

Seeing same issue still. Is there a way to fix the issue with the switches? I am disabling dark mode while the device on dark mode, but the onclor of the switch isn't appears no matter which color it is set with. If I have a background for the switch or a diffrent color background on the screen I can see the full switch and not only the thumbnail, it is still barely visible, but I need a white background so it is not visible at all.

The switch body is transparent for a 'Off' state only. 'On' state is working fine.