Search code examples
androidmessage

How to change color of Button in Alert Dialog


Here my code for create Dialog,

builder.setMessage(msg).setNeutralButton("Dismiss",dialogClickListener)
                       .setPositiveButton("Edit", dialogClickListener)
                       .setNegativeButton("Delete", dialogClickListener).show();

Is it possible to display dismiss in blue color rather than red?


Solution

  • First, create AlertDialog from builder:

    AlertDialog dialog = builder.create();
    

    Then you can find your button and change color:

    dialog.show(); //Only after .show() was called
    dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(your_color);
    dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(your_color);
    dialog.getButton(dialog.BUTTON_NEUTRAL).setTextColor(your_color);
    

    Here you use next method:

    void setTextColor (int color)
         Sets the text color for all the states (normal, selected, focused) to be this color.
    Parameters
         color  int: A color value in the form 0xAARRGGBB. Do not pass a resource ID. To get a color value from a resource ID, call getColor.