Search code examples
androidxamarin.formsxamarin.android

How to change Background Color of Default Display alert in xamarin.android?


I want to change default background color of display alert,I have tried many questions in different sites Can anyone help me ?


Solution

  • If you do not want to use Rg.Plugins.Popup, In the android, you can achieve it with a AlertDialog and custom style.You can call it by DependencyService.

    [assembly: Dependency(typeof(DeviceOpenAlertService))]
    namespace App2.Droid
    {
        class DeviceOpenAlertService : IDeviceOpenAlertService
        {
            public void Open()
            {
    
                var alert = new AlertDialog
                     .Builder(CrossCurrentActivity.Current.Activity, Resource.Style.MyDialogStyle)
                     .SetTitle("Alert Title")
                     .SetMessage("Do you want to close this application")
                     .SetPositiveButton("Yes", new myButtonClicklistener())
                     .SetNegativeButton("No", new myButtonClicklistener())
                     .Create();
    
                alert.Show();
            }
        }
    }
    

    Add following style to the styles.xml

     <style name="MyDialogStyle" parent="android:Theme.Material.Light.Dialog.NoActionBar">
        <!--Dialog Background Color-->
        <item name="android:colorBackground">#FF0000</item>
    
      </style>
    
      <style name="MyButtonsStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <!-- text color for the button -->
        <item name="android:textColor">#00ff00</item>
      </style>
    

    Call it in the PCL.

     DependencyService.Get<IDeviceOpenAlertService>().Open();
    

    Here is running GIF.

    enter image description here