Share via


changing the text color and background color for Android.Support.V7.App.AlertDialog.Builder C#

Question

Monday, January 4, 2021 10:00 PM

was hoping for some advice on how to change the text color and background color of a Android.Support.V7.App.AlertDialog.Builder popup at runtime. some of the examples online refer to using a separate XML style file but i will need to be able to change it programmatically. im using C#. im thinking i need to maybe have a default xml style file to start the process before i can programmatically change it?

All replies (5)

Friday, January 8, 2021 2:26 AM ✅Answered

You can try the following method:

  dialog.Window.SetBackgroundDrawableResource(Resource.Drawable.background);

The original dialog:

After I changed the background, the result is:


Tuesday, January 5, 2021 7:27 AM

changing the text color and background color for Android.Support.V7.App.AlertDialog.Builder C#

Yes, you can define a style in file styles.xml, for example:

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#FFCC00</item>
    <item name="android:textColorPrimary">#FFFFFF</item>
    <item name="android:background">#5fa3d0</item>
</style>

And use like this:

        mBtn.Click += delegate {
            using (var builder = new AlertDialog.Builder(this, Resource.Style.AppCompatAlertDialogStyle))
            {
                var title = "My dialog";
                builder.SetTitle(title);
                builder.SetMessage("This is my test dialog....");

                builder.SetPositiveButton("OK", OkAction);
                builder.SetNegativeButton("Cancel", CancelAction);
                var myCustomDialog = builder.Create();

                myCustomDialog.Show();
            }

             void OkAction(object sender, DialogClickEventArgs e)
            {
                var myButton = sender as Button; 
                if (myButton != null)
                {
                    //do something on ok selected
                }
            }
             void CancelAction(object sender, DialogClickEventArgs e)
            {
                //do something on cancel selected
            }

        };

The result is:


Xamarin forums are migrating to a new home on Microsoft Q&A! We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A! For more information, please refer to this sticky post.


Tuesday, January 5, 2021 3:04 PM

i will need to be able to change the colors at runtime programmaticaly, i wont have a theme to use. i will have 2 colors via Color objects too. something similar to what i use for a spinner:

color_val = Color.ParseColor("#" + _color.HexVal); _spinUPLanguage.SetBackgroundColor(color_val); _spinUPLanguage.SetPopupBackgroundDrawable(new ColorDrawable(color_val));


Monday, January 18, 2021 7:24 PM

@jezh said: You can try the following method:

  dialog.Window.SetBackgroundDrawableResource(Resource.Drawable.background);

The original dialog:

After I changed the background, the result is:

i wont have a resource available, i will have a Color object or RGB value to change it too


Tuesday, January 19, 2021 3:14 PM

ok, so i got it to work using Color ss = Color(bg.Red, bg.Green, bg.Blue); diag.Window.SetBackgroundDrawable(new ColorDrawable(ss));