Does anyone know how to override the default style for AlertDialog buttons? I've looked through the Android source for themes and styles and experimented with different things but I haven't been able to find a way that works.
What I've got below works for changing the backgrounds, but doesn't do anything with the buttons. myTheme
is applied to the whole <application>
via the manifest. (Some other items were deleted for clarity, but they only relate to the title bar.)
<style name="myTheme" parent="android:Theme">
<item name="android:buttonStyle">@style/customButtonStyle</item>
<item name="android:alertDialogStyle">@style/dialogAlertTheme</item>
</style>
<style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
<item name="android:fullDark">@drawable/dialog_loading_background</item>
<item name="android:topDark">@drawable/dialog_alert_top</item>
<item name="android:centerDark">@drawable/dialog_alert_center</item>
<item name="android:bottomDark">@drawable/dialog_alert_bottom</item>
<!-- this last line makes no difference to the buttons -->
<item name="android:buttonStyle">@style/customButtonStyle</item>
</style>
Any ideas?
If you're using Material Components:
This will keep your theme's colors but replace the style of the buttons, for instance to match
Widget.MaterialComponents.Button.TextButton
.Try this:
I made a method like this that will change the default button colors and background
This could be useful for some people!!!
Given that MrSnowflake's reasoning is inaccurate, I'm taking the freedom to provide another answer.
There is something wrong with the markup in Steve Haley's question, and it is mixing up alertDialogStyle and alertDialogTheme, most likely because of the fact that alertDialogTheme was introduced long after alertDialogStyle was around.
So even if @android:style/Theme.Dialog.Alert is available on your Andoid platform, you still can't utilize its expressional power by hooking a cusomized version back into your own theme unless your Android platform supports an android:alertDialogTheme attribute/item for themes. (It may or may not be the case that such inconsistent Android versions exist, I don't know for sure. But the markup used in the question suggests that it does.)
In the question's markup, the parent="@android:style/Theme.Dialog.Alert" will do nothing except creating the illusion that you're customizing the alert dialog theme when you're really only customizing the alert dialog style.
This is how the markup should look like; not all Android versions support all features.
Customizing the alert dialog style has been around for quite some time but is limited to providing (background) drawables for "fullDark", "topDark" etc.
Customizing the alert dialog theme opens a method to provide attributes such as windowBackground, windowTitleStyle and such, but as stated before, you need an Android version which supports the alertDialogThem attribute/item for themes. I can't figure out exactly when this was introduced but it hasn't been Android 2.2 and Eclipse will tell you anyway...
I don't have the resources to validate MrSnowflake's conclusion that it's impossible to style alert dialog buttons in XML, but unless we're facing one of those somewhat nasty aspects of Android where a feature is really missing, I find it unlikely.
As a matter of fact, what's missing in the question is the most relevant part in this respect, namely
so the conclusion that alert dialog buttons do not obey the Widget.Button is not yet proven from my point of view.
Consolidated conclusion: The abilities to style alert dialogs independently of other widgets is limited in Android but getting more powerful as new versions improve in this respect.
See all other anwsers below.
Incorrect:
I guess you have to implement your own
Dialog
class.You can override "buttonBarButtonStyle" instead of "buttonStyle" like this: