7

I have a dialog, which works just fine for me, but I want to set my background for two buttons in this dialog. The structure of it is quite complicated so I don't want to rewrite it to a custom dialog. But in this case, is there any possibility to set a background(more specifically, is there a way to set a style to positive/negative/neutral buttons)?

lomza
  • 9,412
  • 15
  • 70
  • 85
  • You mean u Want to change the Default Colour from the custom Colour for the Dialog box ??? –  Apr 28 '11 at 07:17
  • 1
    What?... I have a dialog like this: http://img10.imageshack.us/img10/8502/dialogx.png and I want these gray buttons be like this http://www.wpclipart.com/blanks/buttons/glossy_buttons/glossy_button_blank_blue_oval.png. I have a special .xml in drawables, which I assign through the "background" tag to buttons. – lomza Apr 28 '11 at 07:28
  • Look into this [Android Dialog with custom Color and Design](http://stackoverflow.com/a/30388711/782535). – Madan Sapkota May 22 '15 at 05:10
  • look at this question -> https://stackoverflow.com/questions/27520967/how-to-change-the-colour-of-positive-and-negative-button-in-custom-alert-dialog – Uche Dim Sep 18 '17 at 14:07

5 Answers5

17

Fundamentally you want to access the dialog buttons: these (on the standard AlertDialog) currently have ids android.R.id.button1 for positive, android.R.id.button2 for negative, and android.R.id.button3 for neutral.

So for example to set the background image on the neutral button you can do this:

Dialog d;
//
// create your dialog into the variable d
//
((Button)d.findViewById(android.R.id.button3)).setBackgroundResource(R.drawable.new_background);

EDIT: this is if you are using an AlertDialog.Builder to create it. For all I know these button assignments may change in the future, so keep that in mind.

EDIT: The chunk of code below should generate something that looks like what you want. It turns out you have to call show BEFORE you change the background

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("TEST MESSAGE)
        .setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        })
        .setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
((Button)alert.findViewById(android.R.id.button1)).setBackgroundResource(R.drawable.button_border);
Femi
  • 64,273
  • 8
  • 118
  • 148
  • It says "Multiple markers at this line - Button cannot be resolved to a type - Button cannot be resolved to a type - The method findViewById(int) is undefined for the type AlertDialog.Builder". I have `AlertDialog.Builder alert = new AlertDialog.Builder(Activity1.MY); alert.setTitle(R.string.exclude_title);`. Then `alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO } }); ((Button)alert.findViewById(android.R.id.button1)).setBackgroundResource(R.drawable.button_border);` – lomza Apr 28 '11 at 09:50
  • You need `AlertDialog.Builder alert = new AlertDialog.Builder(Activity1.MY); alert.setTitle(R.string.exclude_title);` then `alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO } });`. Then `Dialog d = alert.create();`. Then `((Button)d.findViewById(android.R.id.button1)).setBackgroundResource(R.drawa­ble.button_border);`; – Femi Apr 28 '11 at 09:52
  • I don't get errors now, but the dialog left the same. I tried to set different values, - no effect. – lomza Apr 28 '11 at 10:16
  • Pleeeeaaase, somebody help me! – lomza Apr 28 '11 at 11:02
  • Iomza, can you edit your question with a complete chunk of code, including the section where you actually show the dialog? If the dialog doesn't change I suspect the specific instance you are displaying is not the same one you modified. – Femi Apr 28 '11 at 11:32
  • @Femi, thank you so much! I also set the text appearance(same way as with background) =) But it's kinda odd to me, why changes should be made after calling the show() method... – lomza Apr 28 '11 at 12:58
  • All good. I'm guessing it doesn't inflate the views until you call `show()` so there's nothing to update: On my test device it threw a null pointer exception if I tried to alter the background before calling `show()`; – Femi Apr 28 '11 at 13:30
  • I think the answer here to the similiar problem is better: http://stackoverflow.com/a/8077918/1016462 – tasomaniac Aug 01 '13 at 07:01
  • 1
    Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE); In this way we can get the button from alert dialog. – Ganesh Apr 09 '14 at 06:21
2

Actually there's a better and more reliable way to get the buttons for the Dialog than the one posted by @Femi .

You can use the getButton method:

Button positiveButton = yourDialog.getButton(DialogInterface.BUTTON_POSITIVE);

The DialogInterface provides all the needed constants:

DialogInterface.BUTTON_POSITIVE
DialogInterface.BUTTON_NEUTRAL
DialogInterface.BUTTON_NEGATIVE
Fabio Marcolini
  • 2,315
  • 2
  • 24
  • 30
1

If you want to keep the standard text buttons and change the background of the buttons and the whole dialog you can define your own dialog theme in styles.xml like this:

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:background">@color/colorBackground</item>
</style>

...and then when you build the dialog to set its background like this:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyDialogTheme);
Ivo Stoyanov
  • 16,256
  • 8
  • 62
  • 65
0

Please let me note that this would be bad practice to override the show() method for such problem.

In order to encapsulate the solution into the creation of the dialog itself, best practice is to have the buttons customization within dialog constructor:

class CustomDialog extends AlertDialog
{
    public CustomDialog(final Context context)
    {
        super(context);

        setOnShowListener(new OnShowListener()
        {
            @Override
            public void onShow(DialogInterface dialog)
            {
                Button negativeButton = getButton(DialogInterface.BUTTON_NEGATIVE);  
                Button positiveButton = getButton(DialogInterface.BUTTON_POSITIVE);

                negativeButton.setBackgroundColor(Color.GREEN);
                positiveButton.setBackgroundColor(Color.RED);
            }
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Gabriel
  • 581
  • 4
  • 2
0
AlertDialog alert = builder.create();
alert.show();
Button bn = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
bn.setBackgroundColor(Color.RED);
Button bp = alert.getButton(DialogInterface.BUTTON_POSITIVE);
bp.setBackgroundColor(Color.YELLOW);
Panciz
  • 2,183
  • 2
  • 30
  • 54