Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images for AlertDialog buttons

Is it possible to add drawables to the positive, negative and neutral buttons of an AlertDialog? If yes, then how?

like image 771
Ragunath Jawahar Avatar asked Sep 09 '10 18:09

Ragunath Jawahar


2 Answers

Since onPrepareDialog is deprecated, you can use the onShowListener instead.

Also you should set the Drawable bounds or it will be placed far left.

Output of Code below

Output of Code below

public class MyDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle("My Dialog")
                .setNegativeButton("Cancel", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).setPositiveButton("Play", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).create();
        dialog.setOnShowListener(new OnShowListener() {

            @Override
            public void onShow(DialogInterface dialogInterface) {
                Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);

                // if you do the following it will be left aligned, doesn't look
                // correct
                // button.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_media_play,
                // 0, 0, 0);

                Drawable drawable = getActivity().getResources().getDrawable(
                        android.R.drawable.ic_media_play);

                // set the bounds to place the drawable a bit right
                drawable.setBounds((int) (drawable.getIntrinsicWidth() * 0.5),
                        0, (int) (drawable.getIntrinsicWidth() * 1.5),
                        drawable.getIntrinsicHeight());
                button.setCompoundDrawables(drawable, null, null, null);

                // could modify the placement more here if desired
                // button.setCompoundDrawablePadding();
            }
        });
        return dialog;
    }
}
like image 118
aaronvargas Avatar answered Oct 19 '22 03:10

aaronvargas


After you have built the AlertDialog in onCreateDialog you can use the following code in onPrepareDialog to add an image to the positive button:

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
    AlertDialog alertDialog = (AlertDialog)dialog;
    Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    button.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(
            R.drawable.icon), null, null, null);

}

Trying to add the drawable to the button in the onCreateDialog method does not seem to work.

like image 38
Frank Avatar answered Oct 19 '22 03:10

Frank