... ... ...

Custom theme for AlertDialog not being applied

1.6k Views Asked by At

The common style for AlertDialogs is set in themes.xml

<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="alertDialogTheme">@style/ThemeOverlay.AlertDialog</item>
</style>

And the ThemeOverlay.AlertDialog looks like this:

<style name="ThemeOverlay.AlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
    ...
    <item name="buttonStyle">@style/AlertDialogButton</item>
    ...
</style>

However for one particular dialog in the app I need to use a different buttonStyle (textColor). For this I've created a style with a common style as a parent:

<style name="NewAlertButtonStyle" parent="ThemeOverlay.AlertDialog">
    <item name="buttonStyle">@style/AlertDialogButtonRed</item>
</style>

And then the AlertDialogButtonRed looks like:

<style name="AlertDialogButtonRed" parent="Widget.AppCompat.Button">
    <item name="android:textColor">@color/red</item>
</style>

And this is being set when creating a dialog:

AlertDialog.Builder(ContextThemeWrapper(context, R.style.NewAlertButtonStyle))
                .setTitle(getString(R.string.dialog_title))
                .setMessage(getString(R.dialog_body))
                .setPositiveButton(R.string.ok) { _, _ ->
                    //doing something here
                }.show()

Howevere the style seems not being applied, the button text color is not red. What am I missing?

2

There are 2 best solutions below

2
Alan Deep On

Just set R.style.NewAlertButtonStyle in ContextThemeWrapper

0
Aorlinn On

You are speaking about the control buttons of the dialog itself, right? They are using android:buttonBarButtonStyle, which is the correct style for you to apply in the dialog overlay.

But you may probably already have changed the usage of your AlertDialog material dialogs of some sort. In that case there might be a different style you need to apply (there I'm not sure).