Tuesday, July 12, 2022

[FIXED] How to change color of Button in Alert Dialog

Issue

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.


Answered By - Sheikh
Answer Checked By - Mary Flores (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.