Set the background for the buttons in the dialog box?

I have a dialog that works great for me, but I want to set the background for the two buttons in this dialog. Its structure is quite complex, so I do not want to rewrite it in a user dialog. But in this case, is it possible to set the background (more precisely, is there a way to set the style to positive / negative / neutral buttons)?

+5
source share
5 answers

Basically, you want to access the dialog buttons: they (on the standard AlertDialog) currently have identifiers android.R.id.button1for positive, android.R.id.button2negative and android.R.id.button3neutral.

So, for example, to set the background image to a 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 use AlertDialog.Builder to create it. As far as I know, these buttons may change in the future, so keep that in mind.

EDIT: The code snippet below should generate what looks like what you want. Turns out you need to call the 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);
+14
source

There is actually a better and more reliable way to get buttons for Dialogthan the one that @Femi posted.

You can use the getButton method :

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

DialogInterface contains all the necessary constants:

DialogInterface.BUTTON_POSITIVE
DialogInterface.BUTTON_NEUTRAL
DialogInterface.BUTTON_NEGATIVE
+2
source

, , show() .

, :

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);
            }
        }
    }
}
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);
0

, styles.xml :

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

... , , :

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyDialogTheme);
0

All Articles