Change button color in AlertDialog

后端 未结 16 2305

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

相关标签:
16条回答
  • 2020-12-02 22:52

    Are you referring to the neutral, positive and negative buttons? Or to buttons you included in the layout?

    If you are referring to the former, then yes you can. Check out the Custom Button section in this tutorial. You basically need an XML file that will tell your button which drawable/color to use for each state change. You can then set this XML file as your button's background.

    0 讨论(0)
  • 2020-12-02 22:59

    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

    0 讨论(0)
  • 2020-12-02 23:00

    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>
    
    0 讨论(0)
  • 2020-12-02 23:02

    There are 2 ways to do it:

    1. Via code:
            val builder = AlertDialog.Builder(activity!!)
            ...
            val dialog = builder.create()
                    .apply {
                        setOnShowListener {
                            getButton(Dialog.BUTTON_NEGATIVE)?.setTextColor(...)
                        }
                    }
    
    
    1. Via XML :
        <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
            ...
            <item name="materialAlertDialogTheme">@style/ThemeOverlay.MyApp.MaterialAlertDialog</item>
        </style>
    
        <style name="ThemeOverlay.MyApp.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
            <item name="dialogCornerRadius">6dp</item>
            <item name="buttonBarNegativeButtonStyle">@style/Widget.MyApp.NegativeButton</item>
            <item name="buttonBarPositiveButtonStyle">@style/Widget.MyApp.PositiveButton</item>
        </style>
    
        <style name="Widget.MyApp.NegativeButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
            <item name="materialThemeOverlay">@style/ThemeOverlay.MyApp.NegativeButton</item>
        </style>
    
        <style name="Widget.MyApp.PositiveButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
            <item name="materialThemeOverlay">@style/ThemeOverlay.MyApp.PositiveButton</item>
        </style>
    
        <style name="ThemeOverlay.MyApp.NegativeButton" parent="">
            <item name="colorPrimary">#f00</item>
        </style>
    
        <style name="ThemeOverlay.MyApp.PositiveButton" parent="">
            <item name="colorPrimary">#00f</item>
        </style>
    

    usage:

    AlertDialog.Builder(this).setTitle("title").setMessage("message").setPositiveButton("positive", null)
                    .setNegativeButton("negative", null).show()
    

    Or, if you don't want to have the style as default:

    AlertDialog.Builder(this, R.style.ThemeOverlay_MyApp_MaterialAlertDialog).setTitle("title")
                .setMessage("message").setPositiveButton("positive", null)
                .setNegativeButton("negative", null).show()
    
    0 讨论(0)
  • 2020-12-02 23:03

    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>
    
    0 讨论(0)
  • 2020-12-02 23:09

    Here is how you do it:

    // Initializing a new alert dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.message);
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            doAction();
        }
    });
    builder.setNegativeButton(R.string.cancel, null);
    
    // Create the alert dialog and change Buttons colour
    AlertDialog dialog = builder.create();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface arg0) {
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.red));
            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.blue));
            //dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(getResources().getColor(R.color.black));
        }
    });
    dialog.show();
    
    0 讨论(0)
提交回复
热议问题