Android Question

AlertDialog button text color with support library v24.2.1

I am having problems with the AlertDialog. The buttons doesn't use the accentColor to set the button text color any more.

I am using the newest support library, v24.2.1. I am styling my dialogs in my styles.xml the following way:

<style name="Base.Theme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:alertDialogTheme">@style/Widget.DialogStyle</item>
<item name="alertDialogTheme">@style/Widget.DialogStyle</item>

and the Widget.DialogStyle looks like this:

<style name="Widget.DialogStyle" parent="@style/Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorAccent" tools:targetApi="lollipop">@color/primaryColor</item>
<item name="colorAccent">@color/primaryColor</item>
<item name="android:textColorPrimary">@color/primaryText</item>
<item name="android:textColor">@color/primaryText</item>
<item name="android:background">@color/backgroundColor</item>
<item name="android:textAppearanceLarge">@color/primaryText</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>

On app API's lower than 24 the dialog buttons are colored with the colorAccent but on API 24 this is no more the behaviour (the text is black, should be orange). See the following screenshot.

Is there anybody that knows how to get the accentColor back on the buttons?
Thank you.

Answer Source

For some AlertDialog implementations, the buttons are contained in a ButtonBar and take their style from buttonBarButtonStyle. So you have to override the settings you inherit from the parent theme (Theme.AppCompat.Light.Dialog.Alert).

Add the following item to Widget.DialogStyle:

<item name="buttonBarButtonStyle">@style/MyButtonStyle</item>

and add another style named MyButtonStyle like this:

<style name="MyButtonStyle" parent="Widget.AppCompat.Button.Borderless">
    <!-- Set background drawable and text size of the buttons here
    <item name="android:background">@color/my_dialog_dark</item>-->
    <item name="android:textSize">18sp</item>

    <!-- this is the button text color!    -->
    <item name="android:textColor">@color/primaryColor</item>


Thanks to kirtan403 for pointing this out: you can also use another parent style for the buttons if Widget.AppCompat.Button.Borderless does not meet your requirements.

An example by using Widget.AppCompat.Button.ButtonBar.AlertDialog as parent style for the buttons can be found under the AOSP Issue 220699: colorAccent not applied to AlertDialog buttons on Android N.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download