Hello @Bruno Silva ,
Please define the styles for the dialog under dark and light themes. You can open styles.xaml
in the Resources/values folder in your Android project, then define the styles.
<style name="MyDialogLightStyle" parent="android:Theme.Material.Dialog.NoActionBar">
<!--Dialog Background Color-->
<item name="android:colorBackground">#FF0000</item>
</style>
<style name="MyDialogDarkStyle" parent="android:Theme.Material.Dialog.NoActionBar">
<!--Dialog Background Color-->
<item name="android:colorBackground">#FFF</item>
</style>
Then you can use DependencyService
to call the native Android code. And you can try to obtain theme information by Xamarin.Essentials: App Theme, then set different style for the AlertDialog
.
public class DeviceOpenAlertService : IAlertService
{
public void DisplayAlert()
{
Xamarin.Essentials.AppTheme theme = AppInfo.RequestedTheme;// get the theme
AlertDialog alert;
switch (theme){
case Xamarin.Essentials.AppTheme.Dark:// for dark theme
alert = new AlertDialog
.Builder(MainActivity.Instance, Resource.Style.MyDialogDarkStyle)
.SetTitle("Alert Title")
.SetMessage("Do you want to close this application")
.SetPositiveButton("Yes", (c, ev) =>{// Ok button click task})
.SetNegativeButton("No", (c, ev) =>{...})
.Create();
break;
...//for light theme
}
}
After that , you can call it in forms : DependencyService.Get<IAlertService>().DisplayAlert();
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.