Change button color in AlertDialog

2019-01-11 08:46发布

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

13条回答
Anthone
2楼-- · 2019-01-11 09:18

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);
查看更多
别忘想泡老子
3楼-- · 2019-01-11 09:20

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>
查看更多
放荡不羁爱自由
4楼-- · 2019-01-11 09:30

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));
   }
查看更多
我想做一个坏孩纸
5楼-- · 2019-01-11 09:33
    //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"));
}
查看更多
Fickle 薄情
6楼-- · 2019-01-11 09:34

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>
查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-11 09:34

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>
查看更多
登录 后发表回答