Change dialog button color

You can use the MaterialAlertDialogBuilder provided by the Material Components library which allows the creation of a Material AlertDialog.

Just use:

   new MaterialAlertDialogBuilder(MainActivity.this,
       R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
              .setTitle("Dialog")
              .setMessage("Lorem ipsum dolor ....")
              .setPositiveButton("Ok", /* listener = */ null)
              .setNegativeButton("Cancel", /* listener = */ null)
              .show();

Then define your custom style, using the buttonBarPositiveButtonStyle and buttonBarNegativeButtonStyle attributes:

  <!-- Alert Dialog -->
  <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
     <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
     <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
     <item name="buttonBarNeutralButtonStyle">....</item>
  </style>


  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#FFFFFF</item>
    <item name="backgroundTint">#00f</item>
  </style>

  <style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/colorAccent</item>
    <item name="backgroundTint">@color/secondaryColor</item>
  </style>

enter image description here

Leave a Comment