6

my Xamarin.Forms app (Shell project) keeps automatically switching to dark theme when on an Android phone with dark theme enabled. I don't want this to happen. I tried multiple ways to disable this, but none of them worked. Any idea what's wrong?

The interesting part of the code in AppShell.xaml is:

<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
       xmlns:retrogamez="clr-namespace:RetroGameZ"
       Title="RetroGameZ"
       x:Class="RetroGameZ.AppShell">
       

    <Shell.Resources>
        <ResourceDictionary>
            <Style x:Key="BaseStyle" TargetType="Element">
                <Setter Property="Shell.BackgroundColor" Value="#049DBF" />
                <Setter Property="Shell.ForegroundColor" Value="White" />
                <Setter Property="Shell.TitleColor" Value="White" />
                <Setter Property="Shell.DisabledColor" Value="#03A6A6" />
                <Setter Property="Shell.UnselectedColor" Value="#D3D3D3" />
                <Setter Property="Shell.TabBarBackgroundColor" Value="#049DBF" />
                <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
                <Setter Property="Shell.TabBarUnselectedColor" Value="#D3D3D3"/>
                <Setter Property="Shell.TabBarTitleColor" Value="White"/>
            </Style>
            <Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
            <Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
        </ResourceDictionary>
    </Shell.Resources>

later there are just indvidual components.

Zoedingl
  • 176
  • 1
  • 10
  • I believe more context and details are welcome, in order for others to be able to help you easily. Have you checked https://devblogs.microsoft.com/xamarin/app-themes-xamarin-forms/ ? – Cfun Nov 09 '20 at 17:01
  • All I can see that you are setting colors statically and not using `{AppThemeBinding Dark=..., Light=...}"` – Cfun Nov 09 '20 at 21:21
  • Is there any other dark mode setting in your project? How did you support dark mode? If there is no dark mode setting, you app won't switch to dark theme as there is no difference between dark theme and light theme. – nevermore Nov 10 '20 at 03:14

3 Answers3

10

Found the solution for Android:

In MainActivity.cs, before base.OnCreate(), add this line:

AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
ʞᴉɯ
  • 5,376
  • 7
  • 52
  • 89
6

I tried all solutions above but only the following steps worked for me.

  1. Open MainActivity.cs and add the AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo; as first line of OnCreate method:
protected override void OnCreate(Bundle savedInstanceState)
{
    AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;

    base.OnCreate(savedInstanceState);
   // ... other Xamarin stuff
}
  1. Open Resources/values/styles.xml and add the line <item name="android:forceDarkAllowed">false</item>:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="MainTheme" parent="MainTheme.Base">
    <!-- ... other Xamarin stuff -->
    <item name="android:forceDarkAllowed">false</item>
    <!-- ... other Xamarin stuff -->
  </style>
</resources>

Reference: https://stackoverflow.com/a/64339016/6846888

Ângelo Polotto
  • 8,463
  • 2
  • 36
  • 37
0

Assuming you are using Xamarin.Forms embded feature for themes styling mentionned in https://devblogs.microsoft.com/xamarin/app-themes-xamarin-forms/.

Try setting in your App.cs

App.Current.UserAppTheme = OSAppTheme.Light;

If you don't set anything or if you set

App.Current.UserAppTheme = OSAppTheme.Unspecified;

it will follow your os current theme.

Cfun
  • 8,442
  • 4
  • 30
  • 62
  • Can you share some of your code App.xaml.cs and Shell xaml for a better view of the problem ? Tour xamarin forms package version. Your android target version? Your android style in native project...? – Cfun Nov 09 '20 at 18:13
  • What test did you do? Maybe this has regressed: I just tested this. On a simple ContentPage (no Shell). XForms 5.0.0.2291. It has no effect on the default resources, on either iOS or Android. To be precise, it does update UserAppTheme - which means AppThemeBinding usage will probably work. But any styles that don't override colors will still be using the current system light/dark. I set in App's constructor, prior to setting MainPage. I verified later while a page was being laid out, that the theme was still set to Light. But default colors (and top/bottom of screen) still Dark. – ToolmakerSteve Dec 24 '21 at 20:48