Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
</style>

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>
</style>

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.

like image 832
Christian Leicht Jørgensen Avatar asked Sep 23 '16 05:09

Christian Leicht Jørgensen


Video Answer


2 Answers

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>
</style>

EDIT

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 [email protected] 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.

like image 98
Bö macht Blau Avatar answered Oct 09 '22 21:10

Bö macht Blau


Be sure you are importing the correct AlertDialog:

import android.support.v7.app.AlertDialog

Also try inflating the dialog with another AlertDialog.Builder constructor:

android.support.v7.app.AlertDialog.Builder#Builder(android.content.Context, int)

which means, the second parameter is the style of the dialog:

mDialog = new AlertDialog.Builder(context, R.style.Widget.DialogStyle).create();

EDIT:
Sharing the code, that I use to show alert Dialog:

public AlertDialog showSimpleDialog(Context context, String title, String message, String btnOk, DialogInterface.OnClickListener handler) {
    if (mDialog != null && mDialog.isShowing()) {
        mDialog.dismiss();
        mDialog = null;
    }
    mDialog = new AlertDialog.Builder(context, R.style.AppTheme_Dialog).create();
    mDialog.setTitle(title);
    mDialog.setMessage(message);
    mDialog.setButton(DialogInterface.BUTTON_POSITIVE, btnOk, handler);
    mDialog.setCanceledOnTouchOutside(false);
    mDialog.show();
    return mDialog;
}

and the style:

<style name="AppTheme.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorPrimary</item> //blue
    <item name="android:textColorPrimary">@color/primary_text_material_light</item> //black
    <item name="android:windowMinWidthMajor">97%</item>
    <item name="android:windowMinWidthMinor">97%</item>
</style>

and the buttons are blue. Tested on emulator API 24.

like image 22
R. Zagórski Avatar answered Oct 09 '22 21:10

R. Zagórski