android-layoutandroid-alertdialogmaterial3

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


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()

Solution

  • 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)