0
votes

I am following Respond to system theme change to respond to OS theme in xamarin form app. The app supports both Dark and Light themes with AppThemeBinding. I am not using any custom themes. I am following OS themes only. So I have a Switch which the user would prefer to enable Dark Mode (same as OS). The link suggests the following code to enable specified Mode (e.g Dark Mode).

Application.Current.UserAppTheme = OSAppTheme.Dark;

The above code does nothing, but if I write the above code in App.cs after InitializeComponent() The app changes to Dark Mode.

I Then realized to restart the MainActivity in Android which I did with the help of Dependency.

[assembly: Dependency(typeof(AndroidThemeChanged))]
    public class AndroidThemeChanged : ITheme
        {
            public void OnThemeChanged()
            {
                var activity = CrossCurrentActivity.Current.Activity;
                var intent = GetLauncherActivity();
                activity.Finish();
                activity.StartActivity(intent);
            }
            public static Intent GetLauncherActivity()
            {
                var packageName = AndroidApp.Context.PackageName;
                return AndroidApp.Context.PackageManager.GetLaunchIntentForPackage(packageName);
            }
        }

and Calling it

if (Device.RuntimePlatform == Device.Android)
   DependencyService.Get<ITheme>().OnThemeChanged();

Is there any way to update application theme irrespective of OS theme (Dark/Light) without restarting the MainActivity?

2
Have you tried with getting the current theme on start-up with OSAppTheme currentTheme = Application.Current.RequestedTheme;? docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…Mihail Duchev
Well, That would give me the current theme applied. If I apply OS theme in App.cs at application startup, the app accepts that theme. However, what I want is to change OS theme in my app at run time. (e.g) The OS theme is Light, but the user wants to enable OS Dark theme so I have to apply Application.Current.UserAppTheme = OSAppTheme.Dark; (This doesn't change my app theme on the spot unless I restart my app)ARH
Please update your question, because your last sentence is a bit confusing - to update application to follow the OS theme. What you want is not to change "OS" theme, but to have an independent app theme, which doesn't track the configuration of OS theme.Mihail Duchev

2 Answers

0
votes

My bad, I guess I was wrong. The code in my question works as expected. I don't need to have that Android Service to restart activity. Calling below from any where changes the theme to Dark irrespective of OS theme.

Application.Current.UserAppTheme = OSAppTheme.Dark;

-2
votes

I guess you don't have to restart the MainActivity. As per the documentation here we can react to the theme changes using the Application.Current.RequestedThemeChanged event.

Please make sure to follow the AppThemeBinding markup extension as per the documentation here.

Try the below code.

Application.Current.RequestedThemeChanged += Current_RequestedThemeChanged;

private void Current_RequestedThemeChanged(object sender, AppThemeChangedEventArgs e)
{
    if (e.RequestedTheme == OSAppTheme.Dark)
        Application.Current.UserAppTheme = OSAppTheme.Dark;
    else
        Application.Current.UserAppTheme = OSAppTheme.Light;
}

I hope that helps.