ORIGINAL QUESTION:
My apps statusbar does not seem to react to android system theme changes while the app is loaded.
The statusbar only updates after a restart. Please see videos below:
This video shows the light theme correctly applied to the app's statusbar when loaded. But after loading the app, if the user changes the system theme to dark, the statusbar of the app does not update until the app is restarted.
Light to dark theme example video.
This video shows the dark theme correctly applied to the app's statusbar when loaded. But after loading the app, if the users changes the system theme to light, the statusbar of the app does not update until the app is restarted.
Dark to light theme example video.
Anyone know how to get the statusbar to react to system theme changes while having the app loaded?
UPDATE 1:
The following returns the value of the selected theme in the console, but as soon as I uncomment Application.Current.UserAppTheme = AppTheme.Dark;
no values are returned in the console when I change the system theme.
private void ChangeTheme()
{
Application.Current.RequestedThemeChanged += (s, a) =>
{
AppTheme currentTheme = Application.Current.RequestedTheme;
if (currentTheme is AppTheme.Dark)
{
Console.WriteLine(currentTheme.ToString());
//Application.Current.UserAppTheme = AppTheme.Dark;
}
else if (currentTheme is AppTheme.Light)
{
Console.WriteLine(currentTheme.ToString());
}
};
}
UPDATE 2:
I am using 2 different styles.xml files in Platforms > Android > Resources > values and values-night to set the statusbarcolor.
In your MainActivity.cs
you can override the OnConfigurationChanged()
method and listen to UI configuration changes:
public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
if (Build.VERSION.SdkInt < BuildVersionCodes.R)
{
return;
}
if (newConfig.IsNightModeActive)
{
Window?.SetStatusBarColor(Colors.Black.ToAndroid());
Window?.InsetsController?.SetSystemBarsAppearance(0, (int)WindowInsetsControllerAppearance.LightStatusBars);
}
else
{
Window?.SetStatusBarColor(Colors.White.ToAndroid());
Window?.InsetsController?.SetSystemBarsAppearance((int)WindowInsetsControllerAppearance.LightStatusBars, (int)WindowInsetsControllerAppearance.LightStatusBars);
}
}
You also need to include the ConfigChanges.UiMode
flag in the ConfigurationChanges
attribute (included by default in new MAUI projects):
[Activity(Theme = "@style/MyAppTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
UPDATE
About the RequestedThemeChanged
event and the RequestedTheme
property in your case: When you are setting Application.Current.UserAppTheme = AppTheme.Dark;
you are effectively saying that you want the theme to be set to Dark permanently, so the event doesn't get raised anymore after that, since UserAppTheme
is not set to Unspecified
anymore. See more: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/system-theme-changes#set-the-current-user-theme