How to force light mode in Xamarin.Forms?
Asked Answered
V

2

5

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.

Vashtee answered 3/10, 2020 at 10:24 Comment(2)
Hmm, you can't pass store certification when you ignore the user's preferences. How to customize the theme for dark mode is easy to google.Medieval
@HansPassant - Apple makes it possible to opt out of dark mode. Choosing a Specific Interface Style: Supporting both light and dark appearances is a good practice, but you might have good reasons to opt out of appearance changes wholly or partially in your app. ... You can also disable support for Dark Mode entirely using an Info.plist key..Enjambment
D
19

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

Diamante answered 3/10, 2020 at 11:7 Comment(2)
Faced a new problem [#64184545 while trying to apply your answer, I'll mark this as the answer if it works after my issue get's resolved.Vashtee
Theme.AppCompat.Light.DarkActionBar will work as well.Match
M
7

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);
}
Mince answered 29/8, 2021 at 12:15 Comment(1)
Compiter throw "style attribute 'android:attr/forceDarkAllowed' not found.Exteroceptor

© 2022 - 2024 — McMap. All rights reserved.