Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since <code>onPrepareDialog</code> is deprecated, you can use the <code>onShowListener</code> instead. </p> <p>Also you should set the Drawable bounds or it will be placed far left. </p> <p>Output of Code below</p> <p><img src="https://i.stack.imgur.com/FLlJp.png" alt="Output of Code below"></p> <pre><code>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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload