Alert Dialog Divider Color

ainsophicalainsophical USMember ✭✭

Does anyone have an idea on how to create a custom renderer to change the alert divider color for android in xamarin.forms that will work for pre marshmallow android and disregard for material themes???

Answers

  • NMackayNMackay GBInsider, University mod

    @ainsophical

    You can change that but it's a bit of a pain, you'd need to implement your own alert dialog helper class and access it via the dependency service. That's the way I did it, to style that you'd do the following:

     private static bool AlertConfirm(string title, string content, string confirmButton, string cancelButton,
                Action<bool> callback)
            {
                var alert = new AlertDialog.Builder(Forms.Context);
                alert.SetTitle(title);
                alert.SetIcon(Resource.Drawable.IconDialogDecision);
                alert.SetMessage(content);
                alert.SetPositiveButton(confirmButton, (sender, e) => { callback(true); });
                alert.SetNegativeButton(cancelButton, (sender, e) => { callback(false); });
    
                Device.BeginInvokeOnMainThread(() =>
                {
                    var dialog = alert.Show();
                    dialog.SetCanceledOnTouchOutside(false);
                    dialog.SetCancelable(false);
                    BrandAlertDialog(dialog);
                });
    
                return true;
            }
    
            private static void BrandAlertDialog(Dialog dialog)
            {
                try
                {
                    var resources = dialog.Context.Resources;
                    var color = dialog.Context.Resources.GetColor(Resource.Color.dialog_textcolor);
                    var background = dialog.Context.Resources.GetColor(Resource.Color.dialog_background);
    
                    var alertTitleId = resources.GetIdentifier("alertTitle", "id", "android");
                    var alertTitle = (TextView)dialog.Window.DecorView.FindViewById(alertTitleId);
                    alertTitle.SetTextColor(color); // change title text color
    
                    var titleDividerId = resources.GetIdentifier("titleDivider", "id", "android");
                    var titleDivider = dialog.Window.DecorView.FindViewById(titleDividerId);
                    titleDivider.SetBackgroundColor(background); // change divider color
    
                    var dialogBtnTextColor = dialog.Context.Resources.GetColor(Resource.Color.dialogbutton_textcolor);
    
                    var yesBtn = ((AlertDialog)dialog).GetButton((int)DialogButtonType.Positive);
                    if (!Equals(yesBtn, null))
                    {
                        yesBtn.SetBackgroundResource(Resource.Drawable.dialog_button);
                        yesBtn.SetTextColor(dialogBtnTextColor);
                    }
    
                    var noBtn = ((AlertDialog)dialog).GetButton((int)DialogButtonType.Negative);
                    noBtn.SetBackgroundResource(Resource.Drawable.dialog_button);
                    noBtn.SetTextColor(dialogBtnTextColor);
    
    
                }
                catch
                {
                    //Can't change dialog brand color
                }
            }
    

    As I say, you have to write your own dialog interface and then use the dependency service to call the native code.

    It might be less grief to use something like RG.Plugins.Popup to create and style your own dialog.

    https://github.com/rotorgames/Rg.Plugins.Popup

  • ainsophicalainsophical USMember ✭✭

    Hey Norman! Thanks for the quick reply. What a pain when all google had to do was expose this through xml styling instead of hardcoding it. good grief! Thanks again!

Sign In or Register to comment.