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?
Just set
R.style.NewAlertButtonStyleinContextThemeWrapper