How do I change color of alert dialog of material 3 android xml

136 views Asked by At

I want to change the container color of material 3 alert dialog

I tried to use things from official documentation but I couldn't change the color, background color of container remains pink/purple I am creating alert dialog like this and there are .setTheme or .setBackground methods either

MaterialAlertDialogBuilder(itemView.context as AppCompatActivity)
    .setTitle("Do the thing")
    .setMessage("Are you sure you want to do the thing?")
    .setNeutralButton("Cancel") { dialog, which ->
    }
    .setPositiveButton("Do") { dialog, which ->
        doThing()
    }
    .show()
2

There are 2 answers

2
Hezy Ziv On BEST ANSWER

You can create a custom theme just for the dialog box

<style name="CustomAlertDialogTheme" parent="Theme.Material3.DayNight.Dialog.Alert">
    <item name="colorSurface">@color/new_color</item>
</style>

then set it

MaterialAlertDialogBuilder(itemView.context as AppCompatActivity, R.style.CustomAlertDialogTheme)
0
Asuveroz On

You can use this and retain Material 3 corner radius:

<style name="MaterialAlertDialogStyle" parent="MaterialAlertDialog.Material3">
    <item name="android:backgroundTint">@color/new_color</item>
</style>