nasch
nasch

Reputation: 5498

Android override all button styles except dialogs

I've been overriding the default button style in my app like this:

<style name="ButtonStyle" parent="Base.Widget.AppCompat.Button">
        <item name="android:textAllCaps">false</item>
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">@color/primaryTextContrast</item>
        <item name="android:background">@drawable/button</item>
    </style>

That worked fine until Nougat, but with Nougat there's been a change (a bug fix I think) such that this style also applies to buttons in dialogs, while in previous versions it did not. This has the effect of giving the dialog buttons white text on a white background.

In case it's relevant, button is a 9 patch in drawable and is overridden in drawable-v21:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item android:drawable="?attr/colorPrimary"/>
</ripple>

The primary color is dark, and primaryTextContrast is white. Dialogs do not get dark buttons in Nougat for some reason - they seem to pick up the text color but not the background. So I need to either let the dialog buttons do their default thing, or make the buttons fully inherit the style with a dark background and white text.

Upvotes: 2

Views: 477

Answers (1)

bond
bond

Reputation: 11346

@nasch, Dialog buttons use button bar style. In this scenario, you could do something like

<style name="buttonBarButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">@color/colorPrimary</item>
</style>

In your main style definition, you then define

<style name="MyCustomTheme" parent="Theme.AppCompat">
  <item name="buttonBarButtonStyle">@style/buttonBarButtonStyle</item>
 ...

Hope this helps.

Upvotes: 3

Related Questions