Xamarin app automatically switching to dark mode on Android device with dark mode on

Viewed 3069

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.

3 Answers

Found the solution for Android:

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

AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;

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

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.

Related