How can I change the color of the button(s) in an AlertDialog
in Android?
18 Answers
Here is how I did.
AlertDialog.Builder customBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this,android.R.style.Theme_Dialog));
customBuilder.setTitle(R.string.popup_error_title);
customBuilder.setNegativeButton("Exit application", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyActivity.this.finish();
}
});
AlertDialog dialog = customBuilder.create();
dialog.show();
Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));
}
I find the drawable here
-
2hi, i am also applying the Button's BG Image like this only. but if i apply Image i am getting some unwanted Margin at the bottom of the button. if i use BG color i's working fine. i checked the button image. it fine. so can you suggest any solution for this. – Raj Oct 09 '12 at 07:49
-
-
@Deepak : not yet found any solution. if you found any please let me know. – Raj Oct 11 '12 at 05:25
-
1setBackgroundDrawable(Drawable) is deprecated in API level 16; better to use setBackground(Drawable) if you are not targeting below API level 16. – Engin Yapici Mar 17 '13 at 21:54
-
-
@Deepak for the top margin, check if this is not null and set visibility to GONE: alertDialog.findViewById(android.R.id.title); – Rafael Ruiz Muñoz May 05 '17 at 12:34
Since most people are probably using a DialogFragment by now I ran into some issues and clicked my way through several SO answers to solve those. Let me post my current solution.
I ended up setting the button-background with custom drawables as already suggested several times. However, this was not yet possible in the onCreateDialog
-method of the DialogFragment
. You can either do this e.g. in onStart()
, or (which is what I preferred) in the onShow
-listener of the dialog! Keep in mind though, you need to invalidate your buttons after the changes then.
As for the margins: simple remove the padding in your Drawable-XML for the buttons.
#onCreateDialog in your DialogFragment:
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// setup your dialog here...
builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
// do something
}
});
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
// do something
}
});
final AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE);
// this not working because multiplying white background (e.g. Holo Light) has no effect
//negativeButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
final Drawable negativeButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_red);
final Drawable positiveButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_green);
if (Build.VERSION.SDK_INT >= 16) {
negativeButton.setBackground(negativeButtonDrawable);
positiveButton.setBackground(positiveButtonDrawable);
} else {
negativeButton.setBackgroundDrawable(negativeButtonDrawable);
positiveButton.setBackgroundDrawable(positiveButtonDrawable);
}
negativeButton.invalidate();
positiveButton.invalidate();
}
});
return dialog;
}
Drawable-XML example for a button:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/alert_dialog_button_green_pressed1"
android:endColor="@color/alert_dialog_button_green_pressed2"
android:angle="270" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="@color/alert_dialog_button_green_focused1"
android:startColor="@color/alert_dialog_button_green_focused2"
android:angle="270" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="@color/alert_dialog_button_green1"
android:startColor="@color/alert_dialog_button_green2"
android:angle="270" />
</shape>
</item>
</selector>
Don't forget to define your colors in the res\values\colors.xml
, e.g. like this (I didn't want a gradient, therefore colors 1 & 2 are the same):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="alert_dialog_button_green1">#b4099930</color>
<color name="alert_dialog_button_green2">#b4099930</color>
<color name="alert_dialog_button_green_focused1">#96099930</color>
<color name="alert_dialog_button_green_focused2">#96099930</color>
<color name="alert_dialog_button_green_pressed1">#96099930</color>
<color name="alert_dialog_button_green_pressed2">#96099930</color>
</resources>

- 3,809
- 2
- 33
- 39
I have done by this code it might help you:
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setCancelable(true);
builder1.setTitle("abc");
builder1.setMessage("abcdefg");
builder1.setInverseBackgroundForced(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
Button buttonbackground = alert11.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonbackground.setBackgroundColor(Color.BLUE);
Button buttonbackground1 = alert11.getButton(DialogInterface.BUTTON_POSITIVE);
buttonbackground1.setBackgroundColor(Color.BLUE);

- 1,577
- 1
- 17
- 29
-
Worked for me and It's the easiest way! You can also use this to setTextColor() on the button – Ziad H. Apr 05 '20 at 03:36
I wanted to solve this with themes rather than extra code since it feels cleaner to me to have all the styling-related stuff in the styles.xml. What I did was based on Arade's answer and this other question:
<style name="AlertDialogDanger" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/error</item>
</style>
This will change the color of the button text of any alert dialog you create with style AlertDialogDanger
. To do so:
new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogDanger))
.setMessage("Really delete?")
.setPositiveButton("Delete", null)
.setNegativeButton("Cancel", null)
.create().show();

- 1
- 1

- 2,702
- 26
- 36
-
Can we set different colours for Positive, Negative and Neutral buttons using theme? – Hemant Kaushik Jun 14 '18 at 05:27
Here is some example :
AlertDialog.Builder b = new AlertDialog.Builder(all.this);
b.setMessage("r u wan't 2 exit");
b.setCancelable(false);
b.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
b.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i=new Intent(getBaseContext(), s.class);
startActivity(i);
}
});
AlertDialog a=b.create();
a.show();
Button bq = a.getButton(DialogInterface.BUTTON_NEGATIVE);
bq.setBackgroundColor(Color.BLUE);

- 11,667
- 45
- 59

- 91
- 1
- 5
we can change alert dialog button text colour using Style.
AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.yourDialog);
dialog.setTitle(R.string.title);
dialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//code here
}
});
dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//do here
}
});
dialog.show();
Style.xml
<style name="yourDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorAccent">@color/themeColor</item>
<item name="android:colorPrimary">@color/themeColor</item>
</style>

- 3,904
- 32
- 31
There are 2 ways to do it:
- Via code:
val builder = AlertDialog.Builder(activity!!)
...
val dialog = builder.create()
.apply {
setOnShowListener {
getButton(Dialog.BUTTON_NEGATIVE)?.setTextColor(...)
}
}
- Via XML :
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
...
<item name="materialAlertDialogTheme">@style/ThemeOverlay.MyApp.MaterialAlertDialog</item>
</style>
<style name="ThemeOverlay.MyApp.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="dialogCornerRadius">6dp</item>
<item name="buttonBarNegativeButtonStyle">@style/Widget.MyApp.NegativeButton</item>
<item name="buttonBarPositiveButtonStyle">@style/Widget.MyApp.PositiveButton</item>
</style>
<style name="Widget.MyApp.NegativeButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="materialThemeOverlay">@style/ThemeOverlay.MyApp.NegativeButton</item>
</style>
<style name="Widget.MyApp.PositiveButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="materialThemeOverlay">@style/ThemeOverlay.MyApp.PositiveButton</item>
</style>
<style name="ThemeOverlay.MyApp.NegativeButton" parent="">
<item name="colorPrimary">#f00</item>
</style>
<style name="ThemeOverlay.MyApp.PositiveButton" parent="">
<item name="colorPrimary">#00f</item>
</style>
usage:
AlertDialog.Builder(this).setTitle("title").setMessage("message").setPositiveButton("positive", null)
.setNegativeButton("negative", null).show()
Or, if you don't want to have the style as default:
AlertDialog.Builder(this, R.style.ThemeOverlay_MyApp_MaterialAlertDialog).setTitle("title")
.setMessage("message").setPositiveButton("positive", null)
.setNegativeButton("negative", null).show()

- 114,585
- 152
- 739
- 1,270
Maybe someone already answers this way but in my eye, I didn't found so I prefer this answer that is working great. Remember setTextColor should be applied after dialog.show() otherwise i
dialog.show(); //Only after .show() was called
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(neededColor);
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(neededColor);

- 159
- 2
- 4
I think there are developer who wish to extend the AlertDialog class and define the buttons colors withing class definition. For such purpose you can use this code:
class MyDialog extends AlertDialog {
public MyDialog(final Context context) {
super(context);
setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button negativeButton = getButton(DialogInterface.BUTTON_NEGATIVE);
Button positiveButton = getButton(DialogInterface.BUTTON_POSITIVE);
negativeButton.setBackgroundColor(Color.GREEN);
positiveButton.setBackgroundColor(Color.RED);
}
});
}
}
The color of the buttons and other text can also be changed using appcompat :
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorPrimary">@color/flexdrive_blue_1</item>
<item name="android:textColorPrimary">@color/flexdrive_blue_6</item>
<item name="android:colorAccent">@color/flexdrive_blue_1</item>
<item name="colorPrimaryDark">@color/flexdrive_blue_4</item>
</style>

- 567
- 6
- 14
-
Can we set different colours for Positive, Negative and Neutral buttons using theme? – Hemant Kaushik Jun 14 '18 at 05:26
//el resto
AlertDialog a=alertDialog.create();
cambiar_color_texto_alertdialog(a);
}
public void cambiar_color_texto_alertdialog(AlertDialog a){
a.show();
Button BN = a.getButton(DialogInterface.BUTTON_NEGATIVE);
BN.setTextColor(parseColor("#2E9AFE"));
Button BA = a.getButton(DialogInterface.BUTTON_POSITIVE);
BA.setTextColor(parseColor("#2E9AFE"));
}

- 29
- 1
Here is the perfect solution that worked for me:
AlertDialog.Builder alertDialogBuilder
= new AlertDialog.Builder(DashboardActivity.this);
alertDialogBuilder.setTitle("");
alertDialogBuilder.setMessage("Are you sure you want to Logout?");
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
preferenceManager.logout();
Intent intent = new Intent(DashboardActivity.this,
LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
});
alertDialogBuilder.setNegativeButton("Cancel", null);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Button btnOk = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button btnCancel = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if (btnOk != null && btnCancel != null) {
btnOk.setTextColor(getResources().getColor(R.color.colorGreenButton));
btnCancel.setTextColor(getResources().getColor(R.color.colorGreenButton));
} else {
Log.i(TAG, "LogOut: Buttons of Dialog are null");
}

- 2,346
- 3
- 18
- 34

- 1,650
- 1
- 9
- 18
To Change the Buttons color of the AlertDailog
Code:
// Initialize AlertDialog & AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setTitle(R.String.AlertDialogTitle);
...........
.........
//Build your AlertDialog
AlertDialog Demo_alertDialog= builder.create();
Demo_alertDialog.show();
//For Positive Button:
Button b_pos;
b_pos=Demo_alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if(b_pos!=null){
b_pos.setTextColor(getResources().getColor(R.color.YourColor));
}
//For Neutral Button:
Button b_neu;
b_neu=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
if(b_neu!=null){
b_neu.setTextColor(getResources().getColor(R.color.YourColor));
}
//For Negative Button:
Button b_neg;
b_neg=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b_neg!=null){
b_neg.setTextColor(getResources().getColor(R.color.YourColor));
}

- 1,382
- 2
- 15
- 28
if you are using DialogFragment ( android.app.DialogFragment ) then you can overwrite onStart method to get handle of all the buttons (Positive, Negative and Neutral).
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View eventEditDialogView = View.inflate(this.getActivity(), R.layout.event_edit_dialog,
null);
builder.setTitle(getLocalizedString("edit_event"))
.setView(eventEditDialogView)
.setPositiveButton(getLocalizedString("all_events"), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(getLocalizedString("this_event"), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
return builder.create();
}
@Override
public void onStart() {
super.onStart();
Button positive = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
positive.setTextColor(Color.BLACK);
positive.setBackgroundColor(getResources().getColor(R.color.GrayBGColor));
}
All the above solutions will work with AlertDialog or Dialog created on same activity or Fragment but not on DialogFragment created separately.

- 378
- 2
- 9
Here is how you do it:
// Initializing a new alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.message);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
doAction();
}
});
builder.setNegativeButton(R.string.cancel, null);
// Create the alert dialog and change Buttons colour
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.red));
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.blue));
//dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(getResources().getColor(R.color.black));
}
});
dialog.show();

- 1,409
- 1
- 17
- 24
simply make string Spannable and pass it to setPositveButton or negative button like this
val n=getString(R.string.reject).toSpannable() n.setSpan(ForegroundColorSpan(Color.RED),0,6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
n.setNegativeButton(n)

- 111
- 3
no you cant change the color or images or background of the default buttons of alert boxes. For customization you will need to make you on custom dialog box like this.
public class TryAgainAlert extends Dialog implements OnClickListener
{
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
Intent i = new Intent(getApplicationContext(), MainMenu.class);
finish();
startActivity(i);
return true;
}
return super.onKeyDown(keyCode, event);
}
TextView scores;
Button tryagain,mainmenu,submit;
public TryAgainAlert(Context context) {
super(context);
setContentView(R.layout.tryagainalert);
scores=(TextView)findViewById(R.id.text);
tryagain= (Button) findViewById(R.id.trya);
mainmenu= (Button) findViewById(R.id.submitscore);
submit= (Button) findViewById(R.id.mainmenu);
}
@Override
public void onClick(View v) {
if(v == tryagain)
{
else if (v==mainmenu)
{
}
else if (v == submit)
{
}
}
}
you can do what ever you want with the XML file. I hope it will help. Thanks

- 29,597
- 13
- 90
- 129

- 21
- 1
Are you referring to the neutral, positive and negative buttons? Or to buttons you included in the layout?
If you are referring to the former, then yes you can. Check out the Custom Button section in this tutorial. You basically need an XML file that will tell your button which drawable/color to use for each state change. You can then set this XML file as your button's background.

- 5,179
- 2
- 29
- 29
-
No budy, I have already done that one. But i have to change the Background of the positive, neutral and the negative Button. . . – Apr 30 '11 at 07:29