AlertDialog.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
19import android.content.Context;
20import android.content.DialogInterface;
21import android.database.Cursor;
22import android.graphics.drawable.Drawable;
23import android.os.Bundle;
24import android.os.Message;
25import android.view.KeyEvent;
26import android.view.View;
27import android.widget.AdapterView;
28import android.widget.Button;
29import android.widget.ListAdapter;
30import android.widget.ListView;
31
32import com.android.internal.app.AlertController;
33
34/**
35 * A subclass of Dialog that can display one, two or three buttons. If you only want to
36 * display a String in this dialog box, use the setMessage() method.  If you
37 * want to display a more complex view, look up the FrameLayout called "body"
38 * and add your view to it:
39 *
40 * <pre>
41 * FrameLayout fl = (FrameLayout) findViewById(R.id.body);
42 * fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT));
43 * </pre>
44 */
45public class AlertDialog extends Dialog implements DialogInterface {
46    private AlertController mAlert;
47
48    protected AlertDialog(Context context) {
49        this(context, com.android.internal.R.style.Theme_Dialog_Alert);
50    }
51
52    protected AlertDialog(Context context, int theme) {
53        super(context, theme);
54        mAlert = new AlertController(context, this, getWindow());
55    }
56
57    protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
58        super(context, com.android.internal.R.style.Theme_Dialog_Alert);
59        setCancelable(cancelable);
60        setOnCancelListener(cancelListener);
61        mAlert = new AlertController(context, this, getWindow());
62    }
63
64    /**
65     * Gets one of the buttons used in the dialog.
66     * <p>
67     * If a button does not exist in the dialog, null will be returned.
68     *
69     * @param whichButton The identifier of the button that should be returned.
70     *            For example, this can be
71     *            {@link DialogInterface#BUTTON_POSITIVE}.
72     * @return The button from the dialog, or null if a button does not exist.
73     */
74    public Button getButton(int whichButton) {
75        return mAlert.getButton(whichButton);
76    }
77
78    /**
79     * Gets the list view used in the dialog.
80     *
81     * @return The {@link ListView} from the dialog.
82     */
83    public ListView getListView() {
84        return mAlert.getListView();
85    }
86
87    @Override
88    public void setTitle(CharSequence title) {
89        super.setTitle(title);
90        mAlert.setTitle(title);
91    }
92
93    /**
94     * @see Builder#setCustomTitle(View)
95     */
96    public void setCustomTitle(View customTitleView) {
97        mAlert.setCustomTitle(customTitleView);
98    }
99
100    public void setMessage(CharSequence message) {
101        mAlert.setMessage(message);
102    }
103
104    /**
105     * Set the view to display in that dialog.
106     */
107    public void setView(View view) {
108        mAlert.setView(view);
109    }
110
111    /**
112     * Set the view to display in that dialog, specifying the spacing to appear around that
113     * view.
114     *
115     * @param view The view to show in the content area of the dialog
116     * @param viewSpacingLeft Extra space to appear to the left of {@code view}
117     * @param viewSpacingTop Extra space to appear above {@code view}
118     * @param viewSpacingRight Extra space to appear to the right of {@code view}
119     * @param viewSpacingBottom Extra space to appear below {@code view}
120     */
121    public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,
122            int viewSpacingBottom) {
123        mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);
124    }
125
126    /**
127     * Set a message to be sent when a button is pressed.
128     *
129     * @param whichButton Which button to set the message for, can be one of
130     *            {@link DialogInterface#BUTTON_POSITIVE},
131     *            {@link DialogInterface#BUTTON_NEGATIVE}, or
132     *            {@link DialogInterface#BUTTON_NEUTRAL}
133     * @param text The text to display in positive button.
134     * @param msg The {@link Message} to be sent when clicked.
135     */
136    public void setButton(int whichButton, CharSequence text, Message msg) {
137        mAlert.setButton(whichButton, text, null, msg);
138    }
139
140    /**
141     * Set a listener to be invoked when the positive button of the dialog is pressed.
142     *
143     * @param whichButton Which button to set the listener on, can be one of
144     *            {@link DialogInterface#BUTTON_POSITIVE},
145     *            {@link DialogInterface#BUTTON_NEGATIVE}, or
146     *            {@link DialogInterface#BUTTON_NEUTRAL}
147     * @param text The text to display in positive button.
148     * @param listener The {@link DialogInterface.OnClickListener} to use.
149     */
150    public void setButton(int whichButton, CharSequence text, OnClickListener listener) {
151        mAlert.setButton(whichButton, text, listener, null);
152    }
153
154    /**
155     * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
156     *             {@link DialogInterface#BUTTON_POSITIVE}.
157     */
158    @Deprecated
159    public void setButton(CharSequence text, Message msg) {
160        setButton(BUTTON_POSITIVE, text, msg);
161    }
162
163    /**
164     * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
165     *             {@link DialogInterface#BUTTON_NEGATIVE}.
166     */
167    @Deprecated
168    public void setButton2(CharSequence text, Message msg) {
169        setButton(BUTTON_NEGATIVE, text, msg);
170    }
171
172    /**
173     * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
174     *             {@link DialogInterface#BUTTON_NEUTRAL}.
175     */
176    @Deprecated
177    public void setButton3(CharSequence text, Message msg) {
178        setButton(BUTTON_NEUTRAL, text, msg);
179    }
180
181    /**
182     * Set a listener to be invoked when button 1 of the dialog is pressed.
183     *
184     * @param text The text to display in button 1.
185     * @param listener The {@link DialogInterface.OnClickListener} to use.
186     * @deprecated Use
187     *             {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
188     *             with {@link DialogInterface#BUTTON_POSITIVE}
189     */
190    @Deprecated
191    public void setButton(CharSequence text, final OnClickListener listener) {
192        setButton(BUTTON_POSITIVE, text, listener);
193    }
194
195    /**
196     * Set a listener to be invoked when button 2 of the dialog is pressed.
197     * @param text The text to display in button 2.
198     * @param listener The {@link DialogInterface.OnClickListener} to use.
199     * @deprecated Use
200     *             {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
201     *             with {@link DialogInterface#BUTTON_NEGATIVE}
202     */
203    @Deprecated
204    public void setButton2(CharSequence text, final OnClickListener listener) {
205        setButton(BUTTON_NEGATIVE, text, listener);
206    }
207
208    /**
209     * Set a listener to be invoked when button 3 of the dialog is pressed.
210     * @param text The text to display in button 3.
211     * @param listener The {@link DialogInterface.OnClickListener} to use.
212     * @deprecated Use
213     *             {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
214     *             with {@link DialogInterface#BUTTON_POSITIVE}
215     */
216    @Deprecated
217    public void setButton3(CharSequence text, final OnClickListener listener) {
218        setButton(BUTTON_NEUTRAL, text, listener);
219    }
220
221    /**
222     * Set resId to 0 if you don't want an icon.
223     * @param resId the resourceId of the drawable to use as the icon or 0
224     * if you don't want an icon.
225     */
226    public void setIcon(int resId) {
227        mAlert.setIcon(resId);
228    }
229
230    public void setIcon(Drawable icon) {
231        mAlert.setIcon(icon);
232    }
233
234    public void setInverseBackgroundForced(boolean forceInverseBackground) {
235        mAlert.setInverseBackgroundForced(forceInverseBackground);
236    }
237
238    @Override
239    protected void onCreate(Bundle savedInstanceState) {
240        super.onCreate(savedInstanceState);
241        mAlert.installContent();
242    }
243
244    @Override
245    public boolean onKeyDown(int keyCode, KeyEvent event) {
246        if (mAlert.onKeyDown(keyCode, event)) return true;
247        return super.onKeyDown(keyCode, event);
248    }
249
250    @Override
251    public boolean onKeyUp(int keyCode, KeyEvent event) {
252        if (mAlert.onKeyUp(keyCode, event)) return true;
253        return super.onKeyUp(keyCode, event);
254    }
255
256    public static class Builder {
257        private final AlertController.AlertParams P;
258
259        /**
260         * Constructor using a context for this builder and the {@link AlertDialog} it creates.
261         */
262        public Builder(Context context) {
263            P = new AlertController.AlertParams(context);
264        }
265
266        /**
267         * Set the title using the given resource id.
268         *
269         * @return This Builder object to allow for chaining of calls to set methods
270         */
271        public Builder setTitle(int titleId) {
272            P.mTitle = P.mContext.getText(titleId);
273            return this;
274        }
275
276        /**
277         * Set the title displayed in the {@link Dialog}.
278         *
279         * @return This Builder object to allow for chaining of calls to set methods
280         */
281        public Builder setTitle(CharSequence title) {
282            P.mTitle = title;
283            return this;
284        }
285
286        /**
287         * Set the title using the custom view {@code customTitleView}. The
288         * methods {@link #setTitle(int)} and {@link #setIcon(int)} should be
289         * sufficient for most titles, but this is provided if the title needs
290         * more customization. Using this will replace the title and icon set
291         * via the other methods.
292         *
293         * @param customTitleView The custom view to use as the title.
294         *
295         * @return This Builder object to allow for chaining of calls to set methods
296         */
297        public Builder setCustomTitle(View customTitleView) {
298            P.mCustomTitleView = customTitleView;
299            return this;
300        }
301
302        /**
303         * Set the message to display using the given resource id.
304         *
305         * @return This Builder object to allow for chaining of calls to set methods
306         */
307        public Builder setMessage(int messageId) {
308            P.mMessage = P.mContext.getText(messageId);
309            return this;
310        }
311
312        /**
313         * Set the message to display.
314          *
315         * @return This Builder object to allow for chaining of calls to set methods
316         */
317        public Builder setMessage(CharSequence message) {
318            P.mMessage = message;
319            return this;
320        }
321
322        /**
323         * Set the resource id of the {@link Drawable} to be used in the title.
324         *
325         * @return This Builder object to allow for chaining of calls to set methods
326         */
327        public Builder setIcon(int iconId) {
328            P.mIconId = iconId;
329            return this;
330        }
331
332        /**
333         * Set the {@link Drawable} to be used in the title.
334          *
335         * @return This Builder object to allow for chaining of calls to set methods
336         */
337        public Builder setIcon(Drawable icon) {
338            P.mIcon = icon;
339            return this;
340        }
341
342        /**
343         * Set a listener to be invoked when the positive button of the dialog is pressed.
344         * @param textId The resource id of the text to display in the positive button
345         * @param listener The {@link DialogInterface.OnClickListener} to use.
346         *
347         * @return This Builder object to allow for chaining of calls to set methods
348         */
349        public Builder setPositiveButton(int textId, final OnClickListener listener) {
350            P.mPositiveButtonText = P.mContext.getText(textId);
351            P.mPositiveButtonListener = listener;
352            return this;
353        }
354
355        /**
356         * Set a listener to be invoked when the positive button of the dialog is pressed.
357         * @param text The text to display in the positive button
358         * @param listener The {@link DialogInterface.OnClickListener} to use.
359         *
360         * @return This Builder object to allow for chaining of calls to set methods
361         */
362        public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
363            P.mPositiveButtonText = text;
364            P.mPositiveButtonListener = listener;
365            return this;
366        }
367
368        /**
369         * Set a listener to be invoked when the negative button of the dialog is pressed.
370         * @param textId The resource id of the text to display in the negative button
371         * @param listener The {@link DialogInterface.OnClickListener} to use.
372         *
373         * @return This Builder object to allow for chaining of calls to set methods
374         */
375        public Builder setNegativeButton(int textId, final OnClickListener listener) {
376            P.mNegativeButtonText = P.mContext.getText(textId);
377            P.mNegativeButtonListener = listener;
378            return this;
379        }
380
381        /**
382         * Set a listener to be invoked when the negative button of the dialog is pressed.
383         * @param text The text to display in the negative button
384         * @param listener The {@link DialogInterface.OnClickListener} to use.
385         *
386         * @return This Builder object to allow for chaining of calls to set methods
387         */
388        public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
389            P.mNegativeButtonText = text;
390            P.mNegativeButtonListener = listener;
391            return this;
392        }
393
394        /**
395         * Set a listener to be invoked when the neutral button of the dialog is pressed.
396         * @param textId The resource id of the text to display in the neutral button
397         * @param listener The {@link DialogInterface.OnClickListener} to use.
398         *
399         * @return This Builder object to allow for chaining of calls to set methods
400         */
401        public Builder setNeutralButton(int textId, final OnClickListener listener) {
402            P.mNeutralButtonText = P.mContext.getText(textId);
403            P.mNeutralButtonListener = listener;
404            return this;
405        }
406
407        /**
408         * Set a listener to be invoked when the neutral button of the dialog is pressed.
409         * @param text The text to display in the neutral button
410         * @param listener The {@link DialogInterface.OnClickListener} to use.
411         *
412         * @return This Builder object to allow for chaining of calls to set methods
413         */
414        public Builder setNeutralButton(CharSequence text, final OnClickListener listener) {
415            P.mNeutralButtonText = text;
416            P.mNeutralButtonListener = listener;
417            return this;
418        }
419
420        /**
421         * Sets whether the dialog is cancelable or not default is true.
422         *
423         * @return This Builder object to allow for chaining of calls to set methods
424         */
425        public Builder setCancelable(boolean cancelable) {
426            P.mCancelable = cancelable;
427            return this;
428        }
429
430        /**
431         * Sets the callback that will be called if the dialog is canceled.
432         * @see #setCancelable(boolean)
433         *
434         * @return This Builder object to allow for chaining of calls to set methods
435         */
436        public Builder setOnCancelListener(OnCancelListener onCancelListener) {
437            P.mOnCancelListener = onCancelListener;
438            return this;
439        }
440
441        /**
442         * Sets the callback that will be called if a key is dispatched to the dialog.
443         *
444         * @return This Builder object to allow for chaining of calls to set methods
445         */
446        public Builder setOnKeyListener(OnKeyListener onKeyListener) {
447            P.mOnKeyListener = onKeyListener;
448            return this;
449        }
450
451        /**
452         * Set a list of items to be displayed in the dialog as the content, you will be notified of the
453         * selected item via the supplied listener. This should be an array type i.e. R.array.foo
454         *
455         * @return This Builder object to allow for chaining of calls to set methods
456         */
457        public Builder setItems(int itemsId, final OnClickListener listener) {
458            P.mItems = P.mContext.getResources().getTextArray(itemsId);
459            P.mOnClickListener = listener;
460            return this;
461        }
462
463        /**
464         * Set a list of items to be displayed in the dialog as the content, you will be notified of the
465         * selected item via the supplied listener.
466         *
467         * @return This Builder object to allow for chaining of calls to set methods
468         */
469        public Builder setItems(CharSequence[] items, final OnClickListener listener) {
470            P.mItems = items;
471            P.mOnClickListener = listener;
472            return this;
473        }
474
475        /**
476         * Set a list of items, which are supplied by the given {@link ListAdapter}, to be
477         * displayed in the dialog as the content, you will be notified of the
478         * selected item via the supplied listener.
479         *
480         * @param adapter The {@link ListAdapter} to supply the list of items
481         * @param listener The listener that will be called when an item is clicked.
482         *
483         * @return This Builder object to allow for chaining of calls to set methods
484         */
485        public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) {
486            P.mAdapter = adapter;
487            P.mOnClickListener = listener;
488            return this;
489        }
490
491        /**
492         * Set a list of items, which are supplied by the given {@link Cursor}, to be
493         * displayed in the dialog as the content, you will be notified of the
494         * selected item via the supplied listener.
495         *
496         * @param cursor The {@link Cursor} to supply the list of items
497         * @param listener The listener that will be called when an item is clicked.
498         * @param labelColumn The column name on the cursor containing the string to display
499         *          in the label.
500         *
501         * @return This Builder object to allow for chaining of calls to set methods
502         */
503        public Builder setCursor(final Cursor cursor, final OnClickListener listener,
504                String labelColumn) {
505            P.mCursor = cursor;
506            P.mLabelColumn = labelColumn;
507            P.mOnClickListener = listener;
508            return this;
509        }
510
511        /**
512         * Set a list of items to be displayed in the dialog as the content,
513         * you will be notified of the selected item via the supplied listener.
514         * This should be an array type, e.g. R.array.foo. The list will have
515         * a check mark displayed to the right of the text for each checked
516         * item. Clicking on an item in the list will not dismiss the dialog.
517         * Clicking on a button will dismiss the dialog.
518         *
519         * @param itemsId the resource id of an array i.e. R.array.foo
520         * @param checkedItems specifies which items are checked. It should be null in which case no
521         *        items are checked. If non null it must be exactly the same length as the array of
522         *        items.
523         * @param listener notified when an item on the list is clicked. The dialog will not be
524         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
525         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
526         *
527         * @return This Builder object to allow for chaining of calls to set methods
528         */
529        public Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems,
530                final OnMultiChoiceClickListener listener) {
531            P.mItems = P.mContext.getResources().getTextArray(itemsId);
532            P.mOnCheckboxClickListener = listener;
533            P.mCheckedItems = checkedItems;
534            P.mIsMultiChoice = true;
535            return this;
536        }
537
538        /**
539         * Set a list of items to be displayed in the dialog as the content,
540         * you will be notified of the selected item via the supplied listener.
541         * The list will have a check mark displayed to the right of the text
542         * for each checked item. Clicking on an item in the list will not
543         * dismiss the dialog. Clicking on a button will dismiss the dialog.
544         *
545         * @param items the text of the items to be displayed in the list.
546         * @param checkedItems specifies which items are checked. It should be null in which case no
547         *        items are checked. If non null it must be exactly the same length as the array of
548         *        items.
549         * @param listener notified when an item on the list is clicked. The dialog will not be
550         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
551         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
552         *
553         * @return This Builder object to allow for chaining of calls to set methods
554         */
555        public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,
556                final OnMultiChoiceClickListener listener) {
557            P.mItems = items;
558            P.mOnCheckboxClickListener = listener;
559            P.mCheckedItems = checkedItems;
560            P.mIsMultiChoice = true;
561            return this;
562        }
563
564        /**
565         * Set a list of items to be displayed in the dialog as the content,
566         * you will be notified of the selected item via the supplied listener.
567         * The list will have a check mark displayed to the right of the text
568         * for each checked item. Clicking on an item in the list will not
569         * dismiss the dialog. Clicking on a button will dismiss the dialog.
570         *
571         * @param cursor the cursor used to provide the items.
572         * @param isCheckedColumn specifies the column name on the cursor to use to determine
573         *        whether a checkbox is checked or not. It must return an integer value where 1
574         *        means checked and 0 means unchecked.
575         * @param labelColumn The column name on the cursor containing the string to display in the
576         *        label.
577         * @param listener notified when an item on the list is clicked. The dialog will not be
578         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
579         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
580         *
581         * @return This Builder object to allow for chaining of calls to set methods
582         */
583        public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn,
584                final OnMultiChoiceClickListener listener) {
585            P.mCursor = cursor;
586            P.mOnCheckboxClickListener = listener;
587            P.mIsCheckedColumn = isCheckedColumn;
588            P.mLabelColumn = labelColumn;
589            P.mIsMultiChoice = true;
590            return this;
591        }
592
593        /**
594         * Set a list of items to be displayed in the dialog as the content, you will be notified of
595         * the selected item via the supplied listener. This should be an array type i.e.
596         * R.array.foo The list will have a check mark displayed to the right of the text for the
597         * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a
598         * button will dismiss the dialog.
599         *
600         * @param itemsId the resource id of an array i.e. R.array.foo
601         * @param checkedItem specifies which item is checked. If -1 no items are checked.
602         * @param listener notified when an item on the list is clicked. The dialog will not be
603         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
604         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
605         *
606         * @return This Builder object to allow for chaining of calls to set methods
607         */
608        public Builder setSingleChoiceItems(int itemsId, int checkedItem,
609                final OnClickListener listener) {
610            P.mItems = P.mContext.getResources().getTextArray(itemsId);
611            P.mOnClickListener = listener;
612            P.mCheckedItem = checkedItem;
613            P.mIsSingleChoice = true;
614            return this;
615        }
616
617        /**
618         * Set a list of items to be displayed in the dialog as the content, you will be notified of
619         * the selected item via the supplied listener. The list will have a check mark displayed to
620         * the right of the text for the checked item. Clicking on an item in the list will not
621         * dismiss the dialog. Clicking on a button will dismiss the dialog.
622         *
623         * @param cursor the cursor to retrieve the items from.
624         * @param checkedItem specifies which item is checked. If -1 no items are checked.
625         * @param labelColumn The column name on the cursor containing the string to display in the
626         *        label.
627         * @param listener notified when an item on the list is clicked. The dialog will not be
628         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
629         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
630         *
631         * @return This Builder object to allow for chaining of calls to set methods
632         */
633        public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn,
634                final OnClickListener listener) {
635            P.mCursor = cursor;
636            P.mOnClickListener = listener;
637            P.mCheckedItem = checkedItem;
638            P.mLabelColumn = labelColumn;
639            P.mIsSingleChoice = true;
640            return this;
641        }
642
643        /**
644         * Set a list of items to be displayed in the dialog as the content, you will be notified of
645         * the selected item via the supplied listener. The list will have a check mark displayed to
646         * the right of the text for the checked item. Clicking on an item in the list will not
647         * dismiss the dialog. Clicking on a button will dismiss the dialog.
648         *
649         * @param items the items to be displayed.
650         * @param checkedItem specifies which item is checked. If -1 no items are checked.
651         * @param listener notified when an item on the list is clicked. The dialog will not be
652         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
653         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
654         *
655         * @return This Builder object to allow for chaining of calls to set methods
656         */
657        public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {
658            P.mItems = items;
659            P.mOnClickListener = listener;
660            P.mCheckedItem = checkedItem;
661            P.mIsSingleChoice = true;
662            return this;
663        }
664
665        /**
666         * Set a list of items to be displayed in the dialog as the content, you will be notified of
667         * the selected item via the supplied listener. The list will have a check mark displayed to
668         * the right of the text for the checked item. Clicking on an item in the list will not
669         * dismiss the dialog. Clicking on a button will dismiss the dialog.
670         *
671         * @param adapter The {@link ListAdapter} to supply the list of items
672         * @param checkedItem specifies which item is checked. If -1 no items are checked.
673         * @param listener notified when an item on the list is clicked. The dialog will not be
674         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
675         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
676         *
677         * @return This Builder object to allow for chaining of calls to set methods
678         */
679        public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) {
680            P.mAdapter = adapter;
681            P.mOnClickListener = listener;
682            P.mCheckedItem = checkedItem;
683            P.mIsSingleChoice = true;
684            return this;
685        }
686
687        /**
688         * Sets a listener to be invoked when an item in the list is selected.
689         *
690         * @param listener The listener to be invoked.
691         * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)
692         *
693         * @return This Builder object to allow for chaining of calls to set methods
694         */
695        public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {
696            P.mOnItemSelectedListener = listener;
697            return this;
698        }
699
700        /**
701         * Set a custom view to be the contents of the Dialog. If the supplied view is an instance
702         * of a {@link ListView} the light background will be used.
703         *
704         * @param view The view to use as the contents of the Dialog.
705         *
706         * @return This Builder object to allow for chaining of calls to set methods
707         */
708        public Builder setView(View view) {
709            P.mView = view;
710            P.mViewSpacingSpecified = false;
711            return this;
712        }
713
714        /**
715         * Set a custom view to be the contents of the Dialog, specifying the
716         * spacing to appear around that view. If the supplied view is an
717         * instance of a {@link ListView} the light background will be used.
718         *
719         * @param view The view to use as the contents of the Dialog.
720         * @param viewSpacingLeft Spacing between the left edge of the view and
721         *        the dialog frame
722         * @param viewSpacingTop Spacing between the top edge of the view and
723         *        the dialog frame
724         * @param viewSpacingRight Spacing between the right edge of the view
725         *        and the dialog frame
726         * @param viewSpacingBottom Spacing between the bottom edge of the view
727         *        and the dialog frame
728         * @return This Builder object to allow for chaining of calls to set
729         *         methods
730         *
731         * @hide pending API review
732         */
733        public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
734                int viewSpacingRight, int viewSpacingBottom) {
735            P.mView = view;
736            P.mViewSpacingSpecified = true;
737            P.mViewSpacingLeft = viewSpacingLeft;
738            P.mViewSpacingTop = viewSpacingTop;
739            P.mViewSpacingRight = viewSpacingRight;
740            P.mViewSpacingBottom = viewSpacingBottom;
741            return this;
742        }
743
744        /**
745         * Sets the Dialog to use the inverse background, regardless of what the
746         * contents is.
747         *
748         * @param useInverseBackground Whether to use the inverse background
749         *
750         * @return This Builder object to allow for chaining of calls to set methods
751         */
752        public Builder setInverseBackgroundForced(boolean useInverseBackground) {
753            P.mForceInverseBackground = useInverseBackground;
754            return this;
755        }
756
757        /**
758         * Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not
759         * {@link Dialog#show()} the dialog. This allows the user to do any extra processing
760         * before displaying the dialog. Use {@link #show()} if you don't have any other processing
761         * to do and want this to be created and displayed.
762         */
763        public AlertDialog create() {
764            final AlertDialog dialog = new AlertDialog(P.mContext);
765            P.apply(dialog.mAlert);
766            dialog.setCancelable(P.mCancelable);
767            dialog.setOnCancelListener(P.mOnCancelListener);
768            if (P.mOnKeyListener != null) {
769                dialog.setOnKeyListener(P.mOnKeyListener);
770            }
771            return dialog;
772        }
773
774        /**
775         * Creates a {@link AlertDialog} with the arguments supplied to this builder and
776         * {@link Dialog#show()}'s the dialog.
777         */
778        public AlertDialog show() {
779            AlertDialog dialog = create();
780            dialog.show();
781            return dialog;
782        }
783    }
784
785}
786