Forum Xamarin.Android

Theme Light

I updated the visual studio, and now I have the same theme of the program as on the phone, it is dark on the phone, and I want the program to be light, please help,I tried Application.Current.UserAppTheme = OSAppTheme.Light,but it does not work

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited August 2020

    I tried Application.Current.UserAppTheme = OSAppTheme.Light,but it does not work

    To respond to theme changes on Android you must include the ConfigChanges.UiMode flag in the Activity attribute of your MainActivity class. Try to add the code in activity class and test again.

    [Activity(Label = "App19F_9", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.UiMode ...)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        ...
    }
    

    Check the tutorial:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/system-theme-changes#react-to-theme-changes

  • kingston5555kingston5555 Member ✭✭✭

    I have it.But it does not work

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    Did you define and the consume theme resources in the application?

    Check the tutorial:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/system-theme-changes

  • kingston5555kingston5555 Member ✭✭✭

    No, sorry, I do not know how to do it, I just want that regardless of the theme on the phone my projects had a light theme. Old projects did so, I updated yesterday's visual studio and now everything depends on the system theme on the phone, I want to stop it

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited August 2020

    @kingston5555 said:
    No, sorry, I do not know how to do it, I just want that regardless of the theme on the phone my projects had a light theme. Old projects did so, I updated yesterday's visual studio and now everything depends on the system theme on the phone, I want to stop it

    The light theme for the application requests us to customize the settings. When changing the theme of the phone, the application will not change the theme automatically. To keep the light theme for you application, you just need to set a theme style for your application.

    To set theme for the application:

    try to use AppThemeBinding to set data binding to add styles for the properties.

    <ContentPage ...
                 BackgroundColor="{AppThemeBinding Light={StaticResource LightPageBackgroundColor}, Dark={StaticResource LightPageBackgroundColor}}">
    
        <ContentPage.Resources>
            <ResourceDictionary>
                <Color x:Key="LightPageBackgroundColor">White</Color>
                <!--<Color x:Key="DarkPageBackgroundColor">Black</Color>-->
            </ResourceDictionary>
        </ContentPage.Resources>
    
        <ContentPage.Content>
            ...
        </ContentPage.Content>
    </ContentPage>
    

    To use the AppTheme class, we need to add the flag before the Forms.Init method in the MainActivity class.

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            ...
            Forms.SetFlags(new string[] { "AppTheme_Experimental" });
    
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
    }
    

    Check the tutorial:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/theming

    Sample code you could refer to:
    https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-theming/

  • bubakbubak Member

    Add the line in MainActivity ->OnCreate

    AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;

    (docs.microsoft.com/en-us/answers/questions/231994/disable-dark-mode-in-xamarinforms-48.html)

    few hours of my life burned

Sign In or Register to comment.