Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong><em>The setView() method of the AlertDialog class allows one to specify a custom view for the dialog box. Are there any restrictions as to what controls can be included in this custom view ?</em></strong></p> <p>The <code>setView()</code> method in <a href="http://developer.android.com/reference/android/app/AlertDialog.Builder.html" rel="noreferrer">AlertDialog.Builder</a> takes any class extended from <a href="http://developer.android.com/reference/android/view/View.html" rel="noreferrer">View</a> (see it's sub classes and their sub classes).</p> <p>This means EditTexts, Buttons etc. But also Layouts which extend from viewGroups.</p> <p><strong><em>Also, if we set a custom view, can we still add buttons using setPositiveButton, setNegativeButton etc ?</em></strong></p> <p>Yes, it only affects the body. Buttons are added below the layout.</p> <pre><code>@Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Get the layout inflater LayoutInflater inflater = getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog // layout builder.setView(inflater.inflate(R.layout.YourLayout, null)) .setPositiveButton(AlertDialog.BUTTON_NEGATIVE, "Yes!", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // } }) .setNegativeButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { LoginDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } </code></pre> <p><strong>UPDATE:</strong></p> <p>This answer seem to get some new activity since 2 years ago and some things have changed.</p> <p>I updated the code a little bit to improve formatting and added the following tip because of the current state of best practices.</p> <p><em>The AlertDialog defines the style and structure for your dialog, but you should use a DialogFragment as a container for your dialog. The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object.</em></p> <p>The above example is meant when you extend DialogFragment and create a AlertDialog in the onCreateDialog() callback method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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