(I should mention I have read a lot of answers about the AlertDialog
styles and tried every suggestion I could find).
I have a sample class I'm testing. If I use MaterialAlertDialogBuilder
my button colors are fine on Android 14. If I use AlertDialog.Builder
then my colors are wrong. I'm unsure what I'm configuring wrong on my style.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Alertdialogtest" parent="Theme.MaterialComponents.DayNight.NoActionBar" >
<!-- Primary brand color. -->
<item name="colorPrimary">#ffaacc</item>
<item name="colorPrimaryVariant">@color/teal_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/purple_200</item>
<item name="colorOnSecondary">@color/black</item>
<item name="alertDialogStyle">@style/MyAlertDialogTheme</item>
<item name="materialAlertDialogTheme">@style/MyAlertDialogTheme</item>
</style>
<style name="MyAlertDialogTheme" parent="Theme.MaterialComponents.DayNight.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">@style/alertButton</item>
<item name="buttonBarNeutralButtonStyle">@style/alertButton</item>
<item name="buttonBarPositiveButtonStyle">@style/alertButton</item>
</style>
<style name="alertButton" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#791034</item>
</style>
</resources>
This code has button colors of colorPrimary
AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK") { dialog, which ->
// Respond to positive button press
}
.setNegativeButton("Cancel") { dialog, which ->
// Respond to negative button press
}
.show()
This code has color buttons of the style alertButton
android:textColor
:
MaterialAlertDialogBuilder(this)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK") { dialog, which ->
// Respond to positive button press
}
.setNegativeButton("Cancel") { dialog, which ->
// Respond to negative button press
}
.show()
This is just a simple test app to reproduce the issue, the actual dialog I want to show is from a third party library and I have zero control over their code, only thing I can change is the style of the AlertDialog
. So what am I doing wrong?
I'll answer my own question.
It seems like
<item name="colorAccent">@color/color_accent</item>
Will work on some apps, but on my app, for some unknown reason the only thing that worked was colorOnPrimary
.
Edit: forgot to mention, those are on the alert dialog style. And on Android 5 it needs colorPrimary
.