mauialertbackground-colorandroid-themelight

How to change alert style in .Net MAUI Android?


I am porting an application from Xamarin to .Net MAUI, In Xamarin I was using a light theme in Android, so the alerts had a white background but in MAUI the alerts have a gray background.

How can I change the alert background to have a white background?

enter image description here


Solution

  • To solve this issue first I created a "styles.xml" containing this:

    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
      <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    
      </style>
    </resources>
    

    Then I added the file to the directory Platforms/Android/Resources/values directory:

    enter image description here

    Then I set the Build Action to "AndroidResource"

    enter image description here

    The I edited the file "MainActivity.cs" to set the Theme "AppTheme"

    using Android.App;
    using Android.Content.PM;
    using Android.OS;
    
    namespace MyAppMaui
    {
        [Activity(Theme = "@style/AppTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
        public class MainActivity : MauiAppCompatActivity
        {
        }
    }
    

    Done!, It worked