Which style am I missing for AlertDialog button colors to change? Works fine with MaterialAlertDialog
Asked Answered
D

1

0

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

enter image description here

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

enter image description here

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?

Discordance answered 30/11, 2023 at 18:36 Comment(2)
If I remember it right to change the buttons color you should define the <item name="colorAccent">#[your color]</item> in the alert dialog styleParody
Where at? I tried it in my style and it didn't help.Discordance
D
1

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.

Discordance answered 30/11, 2023 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.