How to force light mode in Xamarin.Forms?

7.5k views Asked by At

My app's UI is designed to use in light mode. But if the phone's default theme is dark mode, my app also switches to dark mode and the UI looks trash. So I want force my app to use light mode. How can I do this?

In my app.xaml file I used UserAppTheme="Light", set Content page's background color to variant white color (ex. #FFF, #F2F3F4) but still it doesn't works. I even tried applying <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> in styles.xml. But if the phone is in the dark mode, all the white like area becomes dark too.

I tested in MIUI 12.

2

There are 2 answers

2
Visual Sharp On BEST ANSWER

iOS update your info.plist:

<key>UIUserInterfaceStyle</key>
<string>Light</string>

Android change the style of MainTheme.Base to DayNight:

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

Original answer & more detail from James Montemagno blog

1
Andrej On

Try this for Android

In Andriod project:

  1. In styles.xml file in <style> element add:
    <item name="android:forceDarkAllowed">false</item>
<style name="MainTheme" parent="MainTheme.Base">
  <item name="android:forceDarkAllowed">false</item>
</style>
  1. In MainActivity.cs in OnCreate method add:
    AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo
protected override void OnCreate(Bundle savedInstanceState)
{
    AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
    base.OnCreate(savedInstanceState);
}