How can I change default dialog button text color in android 5

ID : 20045

viewed : 24

Tags : androidandroid-layoutandroid-5.0-lollipopandroid-alertdialogtextcolorandroid

Top 5 Answer for How can I change default dialog button text color in android 5

vote vote

95

Here's a natural way to do it with styles:

If your AppTheme is inherited from Theme.MaterialComponents, then:

<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">     <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>     <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item> </style>  <style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">     <item name="android:textColor">#f00</item> </style>  <style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">     <item name="android:textColor">#00f</item> </style> 

If your AppTheme is inherited from Theme.AppCompat:

<style name="AlertDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">     <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>     <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item> </style>  <style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">     <item name="android:textColor">#f00</item> </style>  <style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">     <item name="android:textColor">#00f</item> </style> 

Use your AlertDialogTheme in your AppTheme

<item name="alertDialogTheme">@style/AlertDialogTheme</item> 

or in constructor

androidx.appcompat.app.AlertDialog.Builder(context, R.style.AlertDialogTheme) 

or If you are using MaterialAlertDialogBuilder then use

<item name="materialAlertDialogTheme">@style/AlertDialogTheme</item> 
vote vote

80

You can try to create the AlertDialog object first, and then use it to set up to change the color of the button and then show it. (Note that on builder object instead of calling show() we call create() to get the AlertDialog object:

//1. create a dialog object 'dialog' MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", errorMessage);  AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {                  @Override                 public void onClick(DialogInterface dialogInterface, int i) {                     ...                 }              }).create();  //2. now setup to change color of the button dialog.setOnShowListener( new OnShowListener() {     @Override     public void onShow(DialogInterface arg0) {         dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(COLOR_I_WANT);     } });  dialog.show() 

The reason you have to do it on onShow() and cannot just get that button after you create your dialog is that the button would not have been created yet.

I changed AlertDialog.BUTTON_POSITIVE to AlertDialog.BUTTON_NEGATIVE to reflect the change in your question. Although it is odd that "OK" button would be a negative button. Usually it is the positive button.

vote vote

73

The color of the buttons and other text can also be changed via theme:

values-21/styles.xml

<style name="AppTheme" parent="...">   ...   <item name="android:timePickerDialogTheme">@style/AlertDialogCustom</item>   <item name="android:datePickerDialogTheme">@style/AlertDialogCustom</item>   <item name="android:alertDialogTheme">@style/AlertDialogCustom</item> </style>  <style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">   <item name="android:colorPrimary">#00397F</item>   <item name="android:colorAccent">#0AAEEF</item> </style> 

The result:

Dialog Date picker

vote vote

61

The simpliest solution is:

dialog.show(); //Only after .show() was called dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(neededColor); dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(neededColor); 
vote vote

53

There are two ways to change the dialog button color.

Basic Way

If you just want to change in an activity, write the below two lines after alertDialog.show();

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary)); alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark)); 

Recommended

I'll recommend adding a theme for AlertDialog in styles.xml so you don't have to write the same code again and again in each activity/dialog call. You can just create a style and apply that theme on the dialog box. So whenever you want to change the color of AlertDialog box, just change color in styles.xml and all the dialog boxes will be updated in the whole application.

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">     <item name="colorAccent">@color/colorPrimary</item> </style> 

And apply the theme in AlertDialog.Builder

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogTheme); 

Top 3 video Explaining How can I change default dialog button text color in android 5

Related QUESTION?