3

I have set up my app as follows:

build.gradle

implementation 'com.google.android.material:material:1.1.0'

styles.xml

    <style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
        <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
        <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    </style>

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

    <style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
        <item name="android:color">@color/colorPrimary</item>
    </style>

CheckoutFragment.kt

   private fun createConfirmOrderDialog() {
       val builder = AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme)
       builder.setTitle(getString(R.string.confirm_order))
           .setMessage(dialogPrompt)
           .setPositiveButton(R.string.confirm) { dialog, _ ->
               viewModel.placeOrder()
               dialog.dismiss()
           }
           .setNegativeButton(R.string.cancel) { dialog, _ ->
               dialog.dismiss()
           }
       builder.show()
  }

colors.xml

<color name="colorAccent">#F1CB1A</color> // I have this added to the base theme

This setup, however, shows a dialog where the button text is not visible since both text and background is white.

The resulting dialog

How can I fix this?

Adhiambo Oyier
  • 215
  • 1
  • 11
  • I think you're calling the wrong overload of `setPositiveButton`. Try it with `.setPositiveButton(getString(R.string.confirm))`. Likewise for the negative case. – Beko Aug 14 '20 at 12:07
  • 1
    Not really, the button and text are present, and if I change the theme to Appcompat I can see them. – Adhiambo Oyier Aug 14 '20 at 12:22

1 Answers1

8

Use the MaterialAlertDialogBuilder instead of AlertDialog.Builder:

    MaterialAlertDialogBuilder(context)
        .setTitle("Title")
        .setMessage(dialogPrompt)
        .setPositiveButton("OK",listener)
        .show()

The default color of the buttons is based on the colorPrimary color.

If you want to use a custom color you can use:

    MaterialAlertDialogBuilder(context,R.style.AlertDialogTheme)

with this style

<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>

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

<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/....</item>
</style>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841