Change button color in AlertDialog

mtr picture mtr · Nov 4, 2010 · Viewed 81k times · Source

How can I change the color of the button(s) in an AlertDialog in Android?

Answer

jbpin picture jbpin · Nov 10, 2011

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