How to Change Default Dialog Button Text Color in Android 5

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

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.

How to change color of Button in Alert Dialog

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.

how to change the button text color on AlertDialog globally using style?

With the MaterialComponents theme and the MaterialAlertDialogBuilder you can define globally the style using the materialAlertDialogTheme attribute in your app theme.

Something like:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="materialAlertDialogTheme">@style/My_MaterialAlertDialog</item>
</style>

Then you can define a custom style:

  <style name="My_MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<!-- Style for positive button -->
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
<!-- Style for negative button -->
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<!-- Style for neutral button -->
<item name="buttonBarNeutralButtonStyle">@style/NeutralButtonStyle</item>
</style>

with the button style defined by:

  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">@color/primaryDarkColor</item>
</style>

<style name="NegativeButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/primaryDarkColor</item>
</style>

<style name="NueutralButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
....
</style>

With the version 1.1.0 of the library you can also simply override the default color using the materialThemeOverlay in the custom style:

  <style name="My_MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="materialThemeOverlay">@style/DialogButtonOverlay</item>
</style>

<style name="DialogButtonOverlay">
<item name="colorPrimary">@color/...</item>
</style>

How can I change the color of the positive button on alertdialog?

Some small changes will do the thing you want-

val builder: AlertDialog.Builder = AlertDialog.Builder(this)
val dialog: AlertDialog = builder.setTitle("SomeText")
.setMessage("SomeMessage")
.setPositiveButton("OK") {
dialog, which -> dialog.dismiss()
}
.setNegativeButton("Cancel") { dialog, which -> dialog.dismiss()

}
.create()
dialog.show()

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE)

dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.DKGRAY)

hope it will be helpful.

How to change the color of buttons in Alert Dialog?

There is a very simple way to make dialog with custom layout: Please review the code below:

final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_news_description);//Your custom layout
TextView sometextview = dialog.findViewById(R.id.textView);// Textview in your custom layout
Button somebutton = dialog.findViewById(R.id.button_done);// Button in your layout
somebutton.setOnClickListener(new View.OnClickListener() {//on button click listener
@Override
public void onClick(View view) {
//DO your job....
//then...
dialog.dismiss();//dismiss the dialog
}
});
dialog.show();

How to change button text color in AlertDialog

Use alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) to have access to Alerts Buttons.

AlertDialog alertDialog;        
alertDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("title") //AlertDialog title
.setMessage("msg") //AlertDialog description
.setCancelable(true)

//call button
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("RuiterRoute", "Emergency Call triggered by user");
}
})

//cancel button
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("RuiterRoute", "Emergency Call canceld by user");
}
}).show();
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);


Related Topics



Leave a reply



Submit