title: How to enable OS AppTheme Dark/Light in Run time in Xamarin Forms
body: <p>I am following <a href="https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/system-theme-changes" rel="nofollow noreferrer">Respond to system theme change</a> to respond to OS theme in xamarin form app. The app supports both Dark and Light themes with <code>AppThemeBinding</code>. I am not using any custom themes. I am following OS themes only. So I have a <code>Switch</code> 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).</p>
  <pre><code>Application.Current.UserAppTheme = OSAppTheme.Dark;
  </code></pre>
  <p>The above code does nothing, but if I write the above code in App.cs after <code>InitializeComponent()</code> The app changes to <code>Dark Mode</code>.</p>
  <p>I Then realized to restart the <code>MainActivity</code> in Android which I did with the help of Dependency.</p>
  <pre><code>[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);
              }
          }
  </code></pre>
  <p>and Calling it</p>
  <pre><code>if (Device.RuntimePlatform == Device.Android)
     DependencyService.Get&lt;ITheme&gt;().OnThemeChanged();
  </code></pre>
  <p>Is there any way to update application theme irrespective of <code>OS theme</code> (Dark/Light) without restarting the <code>MainActivity</code>?</p>