In Xamarin, how to detect when user is changing theme (dark mode)

I would like to react to a theme changed by the user “outside” the application.

Application.Current.RequestedThemeChanged += (s, a) =>
{
      TLog.Info("[X][AppEvent]", $"Current App theme will be changed to '{a.RequestedTheme}'");
      //Do some UI adjustments
};

According to this documentation, in my MainActivity.cs file, I have added ConfigChanges.UiMode :

[Activity(Label = "DringCat", Icon = "@mipmap/icon", Theme = "@style/MainTheme", ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

If user is changing the theme FROM the app (example : I apply Application.Current.UserAppTheme = UserChoice) the event is triggered.
But, when the user is manually changing the theme in Android top bar (circles settings) nothing happens. So, when he closes the top bar display, my app didn’t react to the changement. I need to wait a “OnResume” native method to check the system current theme and apply the new theme.

Am I doing something wrong ?

All the solutions I found on the web seems to conclude with the documentation (link upon), So I started to think I missed something…

Leave a Comment