blob: 782407262c1986f33cd98822424fe20f7475a556 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Adam Powellc49c1732010-09-28 16:03:15 -070019import com.android.internal.app.AlertController;
20
Tor Norbye7b9c9122013-05-30 16:48:33 -070021import android.annotation.ArrayRes;
22import android.annotation.AttrRes;
23import android.annotation.DrawableRes;
24import android.annotation.StringRes;
Alan Viverette682a4332015-04-10 11:05:50 -070025import android.annotation.StyleRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.DialogInterface;
28import android.database.Cursor;
29import android.graphics.drawable.Drawable;
30import android.os.Bundle;
31import android.os.Message;
Adam Powell2fbf4de62010-09-30 15:46:46 -070032import android.util.TypedValue;
Adam Powellc49c1732010-09-28 16:03:15 -070033import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.view.KeyEvent;
35import android.view.View;
36import android.view.WindowManager;
37import android.widget.AdapterView;
38import android.widget.Button;
39import android.widget.ListAdapter;
40import android.widget.ListView;
41
Alan Viverette49f118e2015-02-23 17:15:27 -080042import com.android.internal.R;
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044/**
45 * A subclass of Dialog that can display one, two or three buttons. If you only want to
46 * display a String in this dialog box, use the setMessage() method. If you
Adam Powellc9006872010-03-18 13:44:45 -070047 * want to display a more complex view, look up the FrameLayout called "custom"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 * and add your view to it:
49 *
50 * <pre>
Adam Powellc9006872010-03-18 13:44:45 -070051 * FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
52 * fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 * </pre>
Alan Viverette49f118e2015-02-23 17:15:27 -080054 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 * <p>The AlertDialog class takes care of automatically setting
56 * {@link WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM
57 * WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} for you based on whether
58 * any views in the dialog return true from {@link View#onCheckIsTextEditor()
59 * View.onCheckIsTextEditor()}. Generally you want this set for a Dialog
60 * without text editors, so that it will be placed on top of the current
61 * input method UI. You can modify this behavior by forcing the flag to your
62 * desired mode after calling {@link #onCreate}.
Joe Fernandez558459f2011-10-13 16:47:36 -070063 *
64 * <div class="special reference">
65 * <h3>Developer Guides</h3>
66 * <p>For more information about creating dialogs, read the
67 * <a href="{@docRoot}guide/topics/ui/dialogs.html">Dialogs</a> developer guide.</p>
68 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 */
70public class AlertDialog extends Dialog implements DialogInterface {
71 private AlertController mAlert;
72
Dianne Hackborne79b5542011-01-27 15:18:46 -080073 /**
74 * Special theme constant for {@link #AlertDialog(Context, int)}: use
75 * the traditional (pre-Holo) alert dialog theme.
Alan Viverette49f118e2015-02-23 17:15:27 -080076 *
77 * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
Dianne Hackborne79b5542011-01-27 15:18:46 -080078 */
Alan Viverette49f118e2015-02-23 17:15:27 -080079 @Deprecated
Dianne Hackborne79b5542011-01-27 15:18:46 -080080 public static final int THEME_TRADITIONAL = 1;
Alan Viverette49f118e2015-02-23 17:15:27 -080081
Dianne Hackborne79b5542011-01-27 15:18:46 -080082 /**
83 * Special theme constant for {@link #AlertDialog(Context, int)}: use
84 * the holographic alert theme with a dark background.
Alan Viverette49f118e2015-02-23 17:15:27 -080085 *
86 * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
Dianne Hackborne79b5542011-01-27 15:18:46 -080087 */
Alan Viverette49f118e2015-02-23 17:15:27 -080088 @Deprecated
Dianne Hackborne79b5542011-01-27 15:18:46 -080089 public static final int THEME_HOLO_DARK = 2;
Alan Viverette49f118e2015-02-23 17:15:27 -080090
Dianne Hackborne79b5542011-01-27 15:18:46 -080091 /**
92 * Special theme constant for {@link #AlertDialog(Context, int)}: use
93 * the holographic alert theme with a light background.
Alan Viverette49f118e2015-02-23 17:15:27 -080094 *
95 * @deprecated Use {@link android.R.style#Theme_Material_Light_Dialog_Alert}.
Dianne Hackborne79b5542011-01-27 15:18:46 -080096 */
Alan Viverette49f118e2015-02-23 17:15:27 -080097 @Deprecated
Dianne Hackborne79b5542011-01-27 15:18:46 -080098 public static final int THEME_HOLO_LIGHT = 3;
Adam Powell6e90a362011-08-14 16:48:32 -070099
100 /**
101 * Special theme constant for {@link #AlertDialog(Context, int)}: use
102 * the device's default alert theme with a dark background.
Alan Viverette49f118e2015-02-23 17:15:27 -0800103 *
104 * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Dialog_Alert}.
Adam Powell6e90a362011-08-14 16:48:32 -0700105 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800106 @Deprecated
Adam Powell6e90a362011-08-14 16:48:32 -0700107 public static final int THEME_DEVICE_DEFAULT_DARK = 4;
108
109 /**
110 * Special theme constant for {@link #AlertDialog(Context, int)}: use
Scott Kennedy7ed189e2013-01-11 22:31:43 -0800111 * the device's default alert theme with a light background.
Alan Viverette49f118e2015-02-23 17:15:27 -0800112 *
113 * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Light_Dialog_Alert}.
Adam Powell6e90a362011-08-14 16:48:32 -0700114 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800115 @Deprecated
Adam Powell6e90a362011-08-14 16:48:32 -0700116 public static final int THEME_DEVICE_DEFAULT_LIGHT = 5;
Craig Stout4c0cb8a2014-04-04 13:03:10 -0700117
118 /**
119 * No layout hint.
120 * @hide
121 */
122 public static final int LAYOUT_HINT_NONE = 0;
123
124 /**
125 * Hint layout to the side.
126 * @hide
127 */
128 public static final int LAYOUT_HINT_SIDE = 1;
Alan Viverette49f118e2015-02-23 17:15:27 -0800129
130 /**
131 * Creates an alert dialog that uses the default alert dialog theme.
132 * <p>
133 * The default alert dialog theme is defined by
134 * {@link android.R.attr#alertDialogTheme} within the parent
135 * {@code context}'s theme.
136 *
137 * @param context the parent context
Alan Viverette5696f042015-04-08 11:03:32 -0700138 * @see android.R.styleable#Theme_alertDialogTheme
Alan Viverette49f118e2015-02-23 17:15:27 -0800139 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 protected AlertDialog(Context context) {
Alan Viverette49f118e2015-02-23 17:15:27 -0800141 this(context, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 }
143
Dianne Hackborne79b5542011-01-27 15:18:46 -0800144 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800145 * Creates an alert dialog that uses the default alert dialog theme and a
146 * custom cancel listener.
147 * <p>
148 * This is functionally identical to:
149 * <pre>
150 * AlertDialog dialog = new AlertDialog(context);
151 * alertDialog.setCancelable(cancelable);
152 * alertDialog.setOnCancelListener(cancelListener);
153 * </pre>
154 * <p>
155 * The default alert dialog theme is defined by
156 * {@link android.R.attr#alertDialogTheme} within the parent
157 * {@code context}'s theme.
158 *
159 * @param context the parent context
Alan Viverette5696f042015-04-08 11:03:32 -0700160 * @see android.R.styleable#Theme_alertDialogTheme
Dianne Hackborne79b5542011-01-27 15:18:46 -0800161 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800162 protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
163 this(context, 0);
164
165 setCancelable(cancelable);
166 setOnCancelListener(cancelListener);
Dianne Hackborne79b5542011-01-27 15:18:46 -0800167 }
168
Alan Viverette49f118e2015-02-23 17:15:27 -0800169 /**
170 * Creates an alert dialog that uses an explicit theme resource.
171 * <p>
172 * The specified theme resource ({@code themeResId}) is applied on top of
173 * the parent {@code context}'s theme. It may be specified as a style
174 * resource containing a fully-populated theme, such as
175 * {@link android.R.style#Theme_Material_Dialog}, to replace all attributes
176 * in the parent {@code context}'s theme including primary and accent
177 * colors.
178 * <p>
179 * To preserve attributes such as primary and accent colors, the
180 * {@code themeResId} may instead be specified as an overlay theme such as
181 * {@link android.R.style#ThemeOverlay_Material_Dialog}. This will override
182 * only the window attributes necessary to style the alert window as a
183 * dialog.
184 * <p>
185 * Alternatively, the {@code themeResId} may be specified as {@code 0} to
186 * use the parent {@code context}'s resolved value for
187 * {@link android.R.attr#alertDialogTheme}.
188 *
189 * @param context the parent context
190 * @param themeResId the resource ID of the theme against which to inflate
191 * this dialog, or {@code 0} to use the parent
192 * {@code context}'s default alert dialog theme
Alan Viverette5696f042015-04-08 11:03:32 -0700193 * @see android.R.styleable#Theme_alertDialogTheme
Alan Viverette49f118e2015-02-23 17:15:27 -0800194 */
Alan Viverette682a4332015-04-10 11:05:50 -0700195 protected AlertDialog(Context context, @StyleRes int themeResId) {
Alan Viverette5696f042015-04-08 11:03:32 -0700196 this(context, themeResId, true);
197 }
198
Alan Viverette682a4332015-04-10 11:05:50 -0700199 AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
Alan Viverette5696f042015-04-08 11:03:32 -0700200 super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0,
201 createContextThemeWrapper);
Jeff Browna492c3a2012-08-23 19:48:44 -0700202
Dianne Hackborncfaf8872011-01-18 13:57:54 -0800203 mWindow.alwaysReadCloseOnTouchAttr();
Dianne Hackborne79b5542011-01-27 15:18:46 -0800204 mAlert = new AlertController(getContext(), this, getWindow());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
206
Alan Viverette49f118e2015-02-23 17:15:27 -0800207 static int resolveDialogTheme(Context context, int themeResId) {
Alan Viverette682a4332015-04-10 11:05:50 -0700208 if (themeResId == THEME_TRADITIONAL) {
Alan Viverette49f118e2015-02-23 17:15:27 -0800209 return R.style.Theme_Dialog_Alert;
210 } else if (themeResId == THEME_HOLO_DARK) {
211 return R.style.Theme_Holo_Dialog_Alert;
212 } else if (themeResId == THEME_HOLO_LIGHT) {
213 return R.style.Theme_Holo_Light_Dialog_Alert;
214 } else if (themeResId == THEME_DEVICE_DEFAULT_DARK) {
215 return R.style.Theme_DeviceDefault_Dialog_Alert;
216 } else if (themeResId == THEME_DEVICE_DEFAULT_LIGHT) {
217 return R.style.Theme_DeviceDefault_Light_Dialog_Alert;
218 } else if (themeResId >= 0x01000000) { // start of real resource IDs.
219 return themeResId;
Dianne Hackborne79b5542011-01-27 15:18:46 -0800220 } else {
Alan Viverette49f118e2015-02-23 17:15:27 -0800221 final TypedValue outValue = new TypedValue();
222 context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true);
Dianne Hackborne79b5542011-01-27 15:18:46 -0800223 return outValue.resourceId;
224 }
Adam Powell2fbf4de62010-09-30 15:46:46 -0700225 }
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 /**
Alan Viverettea1e63312013-12-09 18:20:19 -0800228 * Gets one of the buttons used in the dialog. Returns null if the specified
Alan Viverettef34ef742013-12-11 15:59:53 -0800229 * button does not exist or the dialog has not yet been fully created (for
230 * example, via {@link #show()} or {@link #create()}).
Alan Viverettea1e63312013-12-09 18:20:19 -0800231 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * @param whichButton The identifier of the button that should be returned.
233 * For example, this can be
234 * {@link DialogInterface#BUTTON_POSITIVE}.
235 * @return The button from the dialog, or null if a button does not exist.
236 */
237 public Button getButton(int whichButton) {
238 return mAlert.getButton(whichButton);
239 }
Alan Viverettea1e63312013-12-09 18:20:19 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
242 * Gets the list view used in the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800243 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 * @return The {@link ListView} from the dialog.
245 */
246 public ListView getListView() {
247 return mAlert.getListView();
248 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 @Override
251 public void setTitle(CharSequence title) {
252 super.setTitle(title);
253 mAlert.setTitle(title);
254 }
255
256 /**
257 * @see Builder#setCustomTitle(View)
258 */
259 public void setCustomTitle(View customTitleView) {
260 mAlert.setCustomTitle(customTitleView);
261 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 public void setMessage(CharSequence message) {
264 mAlert.setMessage(message);
265 }
266
267 /**
268 * Set the view to display in that dialog.
269 */
270 public void setView(View view) {
271 mAlert.setView(view);
272 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800275 * Set the view to display in that dialog, specifying the spacing to appear around that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 * view.
277 *
278 * @param view The view to show in the content area of the dialog
279 * @param viewSpacingLeft Extra space to appear to the left of {@code view}
280 * @param viewSpacingTop Extra space to appear above {@code view}
281 * @param viewSpacingRight Extra space to appear to the right of {@code view}
282 * @param viewSpacingBottom Extra space to appear below {@code view}
283 */
284 public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,
285 int viewSpacingBottom) {
286 mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);
287 }
288
289 /**
Craig Stout4c0cb8a2014-04-04 13:03:10 -0700290 * Internal api to allow hinting for the best button panel layout.
291 * @hide
292 */
293 void setButtonPanelLayoutHint(int layoutHint) {
294 mAlert.setButtonPanelLayoutHint(layoutHint);
295 }
296
297 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 * Set a message to be sent when a button is pressed.
Alan Viverette49f118e2015-02-23 17:15:27 -0800299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 * @param whichButton Which button to set the message for, can be one of
301 * {@link DialogInterface#BUTTON_POSITIVE},
302 * {@link DialogInterface#BUTTON_NEGATIVE}, or
303 * {@link DialogInterface#BUTTON_NEUTRAL}
304 * @param text The text to display in positive button.
305 * @param msg The {@link Message} to be sent when clicked.
306 */
307 public void setButton(int whichButton, CharSequence text, Message msg) {
308 mAlert.setButton(whichButton, text, null, msg);
309 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 /**
312 * Set a listener to be invoked when the positive button of the dialog is pressed.
Alan Viverette49f118e2015-02-23 17:15:27 -0800313 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 * @param whichButton Which button to set the listener on, can be one of
315 * {@link DialogInterface#BUTTON_POSITIVE},
316 * {@link DialogInterface#BUTTON_NEGATIVE}, or
317 * {@link DialogInterface#BUTTON_NEUTRAL}
318 * @param text The text to display in positive button.
319 * @param listener The {@link DialogInterface.OnClickListener} to use.
320 */
321 public void setButton(int whichButton, CharSequence text, OnClickListener listener) {
322 mAlert.setButton(whichButton, text, listener, null);
323 }
324
325 /**
326 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
327 * {@link DialogInterface#BUTTON_POSITIVE}.
328 */
329 @Deprecated
330 public void setButton(CharSequence text, Message msg) {
331 setButton(BUTTON_POSITIVE, text, msg);
332 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
335 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
336 * {@link DialogInterface#BUTTON_NEGATIVE}.
337 */
338 @Deprecated
339 public void setButton2(CharSequence text, Message msg) {
340 setButton(BUTTON_NEGATIVE, text, msg);
341 }
342
343 /**
344 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
345 * {@link DialogInterface#BUTTON_NEUTRAL}.
346 */
347 @Deprecated
348 public void setButton3(CharSequence text, Message msg) {
349 setButton(BUTTON_NEUTRAL, text, msg);
350 }
351
352 /**
353 * Set a listener to be invoked when button 1 of the dialog is pressed.
Alan Viverette49f118e2015-02-23 17:15:27 -0800354 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * @param text The text to display in button 1.
356 * @param listener The {@link DialogInterface.OnClickListener} to use.
357 * @deprecated Use
358 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
359 * with {@link DialogInterface#BUTTON_POSITIVE}
360 */
361 @Deprecated
362 public void setButton(CharSequence text, final OnClickListener listener) {
363 setButton(BUTTON_POSITIVE, text, listener);
364 }
365
366 /**
367 * Set a listener to be invoked when button 2 of the dialog is pressed.
368 * @param text The text to display in button 2.
369 * @param listener The {@link DialogInterface.OnClickListener} to use.
370 * @deprecated Use
371 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
372 * with {@link DialogInterface#BUTTON_NEGATIVE}
373 */
374 @Deprecated
375 public void setButton2(CharSequence text, final OnClickListener listener) {
376 setButton(BUTTON_NEGATIVE, text, listener);
377 }
378
379 /**
380 * Set a listener to be invoked when button 3 of the dialog is pressed.
381 * @param text The text to display in button 3.
382 * @param listener The {@link DialogInterface.OnClickListener} to use.
383 * @deprecated Use
384 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
385 * with {@link DialogInterface#BUTTON_POSITIVE}
386 */
387 @Deprecated
388 public void setButton3(CharSequence text, final OnClickListener listener) {
389 setButton(BUTTON_NEUTRAL, text, listener);
390 }
391
392 /**
393 * Set resId to 0 if you don't want an icon.
394 * @param resId the resourceId of the drawable to use as the icon or 0
395 * if you don't want an icon.
396 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700397 public void setIcon(@DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 mAlert.setIcon(resId);
399 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 public void setIcon(Drawable icon) {
402 mAlert.setIcon(icon);
403 }
404
Adam Powell947f7822011-01-07 22:30:48 -0800405 /**
406 * Set an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon
407 *
408 * @param attrId ID of a theme attribute that points to a drawable resource.
409 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700410 public void setIconAttribute(@AttrRes int attrId) {
Adam Powell947f7822011-01-07 22:30:48 -0800411 TypedValue out = new TypedValue();
412 mContext.getTheme().resolveAttribute(attrId, out, true);
413 mAlert.setIcon(out.resourceId);
414 }
415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 public void setInverseBackgroundForced(boolean forceInverseBackground) {
417 mAlert.setInverseBackgroundForced(forceInverseBackground);
418 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 @Override
421 protected void onCreate(Bundle savedInstanceState) {
422 super.onCreate(savedInstanceState);
423 mAlert.installContent();
424 }
425
426 @Override
427 public boolean onKeyDown(int keyCode, KeyEvent event) {
428 if (mAlert.onKeyDown(keyCode, event)) return true;
429 return super.onKeyDown(keyCode, event);
430 }
431
432 @Override
433 public boolean onKeyUp(int keyCode, KeyEvent event) {
434 if (mAlert.onKeyUp(keyCode, event)) return true;
435 return super.onKeyUp(keyCode, event);
436 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 public static class Builder {
439 private final AlertController.AlertParams P;
Alan Viverette49f118e2015-02-23 17:15:27 -0800440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800442 * Creates a builder for an alert dialog that uses the default alert
443 * dialog theme.
444 * <p>
445 * The default alert dialog theme is defined by
446 * {@link android.R.attr#alertDialogTheme} within the parent
447 * {@code context}'s theme.
448 *
449 * @param context the parent context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 */
451 public Builder(Context context) {
Dianne Hackborne79b5542011-01-27 15:18:46 -0800452 this(context, resolveDialogTheme(context, 0));
Martin Nordholtse429c5e2010-06-15 16:17:24 +0200453 }
454
455 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800456 * Creates a builder for an alert dialog that uses an explicit theme
457 * resource.
458 * <p>
459 * The specified theme resource ({@code themeResId}) is applied on top
460 * of the parent {@code context}'s theme. It may be specified as a
461 * style resource containing a fully-populated theme, such as
462 * {@link android.R.style#Theme_Material_Dialog}, to replace all
463 * attributes in the parent {@code context}'s theme including primary
464 * and accent colors.
465 * <p>
466 * To preserve attributes such as primary and accent colors, the
467 * {@code themeResId} may instead be specified as an overlay theme such
468 * as {@link android.R.style#ThemeOverlay_Material_Dialog}. This will
469 * override only the window attributes necessary to style the alert
470 * window as a dialog.
471 * <p>
472 * Alternatively, the {@code themeResId} may be specified as {@code 0}
473 * to use the parent {@code context}'s resolved value for
474 * {@link android.R.attr#alertDialogTheme}.
475 *
476 * @param context the parent context
477 * @param themeResId the resource ID of the theme against which to inflate
478 * this dialog, or {@code 0} to use the parent
479 * {@code context}'s default alert dialog theme
Martin Nordholtse429c5e2010-06-15 16:17:24 +0200480 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800481 public Builder(Context context, int themeResId) {
Dianne Hackborne79b5542011-01-27 15:18:46 -0800482 P = new AlertController.AlertParams(new ContextThemeWrapper(
Alan Viverette49f118e2015-02-23 17:15:27 -0800483 context, resolveDialogTheme(context, themeResId)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 /**
Adam Powellc49c1732010-09-28 16:03:15 -0700487 * Returns a {@link Context} with the appropriate theme for dialogs created by this Builder.
488 * Applications should use this Context for obtaining LayoutInflaters for inflating views
489 * that will be used in the resulting dialogs, as it will cause views to be inflated with
490 * the correct theme.
491 *
492 * @return A Context for built Dialogs.
493 */
494 public Context getContext() {
Adam Powellccb013f2010-10-12 18:42:56 -0700495 return P.mContext;
Adam Powellc49c1732010-09-28 16:03:15 -0700496 }
497
498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 * Set the title using the given resource id.
500 *
501 * @return This Builder object to allow for chaining of calls to set methods
502 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700503 public Builder setTitle(@StringRes int titleId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 P.mTitle = P.mContext.getText(titleId);
505 return this;
506 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 /**
509 * Set the title displayed in the {@link Dialog}.
510 *
511 * @return This Builder object to allow for chaining of calls to set methods
512 */
513 public Builder setTitle(CharSequence title) {
514 P.mTitle = title;
515 return this;
516 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800519 * Set the title using the custom view {@code customTitleView}.
520 * <p>
521 * The methods {@link #setTitle(int)} and {@link #setIcon(int)} should
522 * be sufficient for most titles, but this is provided if the title
523 * needs more customization. Using this will replace the title and icon
524 * set via the other methods.
525 * <p>
526 * <strong>Note:</strong> To ensure consistent styling, the custom view
527 * should be inflated or constructed using the alert dialog's themed
528 * context obtained via {@link #getContext()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 *
Alan Viverette49f118e2015-02-23 17:15:27 -0800530 * @param customTitleView the custom view to use as the title
531 * @return this Builder object to allow for chaining of calls to set
532 * methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 */
534 public Builder setCustomTitle(View customTitleView) {
535 P.mCustomTitleView = customTitleView;
536 return this;
537 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 /**
540 * Set the message to display using the given resource id.
541 *
542 * @return This Builder object to allow for chaining of calls to set methods
543 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700544 public Builder setMessage(@StringRes int messageId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 P.mMessage = P.mContext.getText(messageId);
546 return this;
547 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 /**
550 * Set the message to display.
551 *
552 * @return This Builder object to allow for chaining of calls to set methods
553 */
554 public Builder setMessage(CharSequence message) {
555 P.mMessage = message;
556 return this;
557 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 /**
560 * Set the resource id of the {@link Drawable} to be used in the title.
Alan Viverette5c2d8f72015-01-05 12:56:45 -0800561 * <p>
562 * Takes precedence over values set using {@link #setIcon(Drawable)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 *
564 * @return This Builder object to allow for chaining of calls to set methods
565 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700566 public Builder setIcon(@DrawableRes int iconId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 P.mIconId = iconId;
568 return this;
569 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 /**
572 * Set the {@link Drawable} to be used in the title.
Alan Viverette49f118e2015-02-23 17:15:27 -0800573 * <p>
574 * <strong>Note:</strong> To ensure consistent styling, the drawable
575 * should be inflated or constructed using the alert dialog's themed
576 * context obtained via {@link #getContext()}.
577 *
578 * @return this Builder object to allow for chaining of calls to set
579 * methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 */
581 public Builder setIcon(Drawable icon) {
582 P.mIcon = icon;
583 return this;
584 }
Adam Powell947f7822011-01-07 22:30:48 -0800585
586 /**
Alan Viverette5c2d8f72015-01-05 12:56:45 -0800587 * Set an icon as supplied by a theme attribute. e.g.
588 * {@link android.R.attr#alertDialogIcon}.
589 * <p>
590 * Takes precedence over values set using {@link #setIcon(int)} or
591 * {@link #setIcon(Drawable)}.
Adam Powell947f7822011-01-07 22:30:48 -0800592 *
593 * @param attrId ID of a theme attribute that points to a drawable resource.
594 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700595 public Builder setIconAttribute(@AttrRes int attrId) {
Adam Powell947f7822011-01-07 22:30:48 -0800596 TypedValue out = new TypedValue();
597 P.mContext.getTheme().resolveAttribute(attrId, out, true);
598 P.mIconId = out.resourceId;
599 return this;
600 }
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 /**
603 * Set a listener to be invoked when the positive button of the dialog is pressed.
604 * @param textId The resource id of the text to display in the positive button
605 * @param listener The {@link DialogInterface.OnClickListener} to use.
606 *
607 * @return This Builder object to allow for chaining of calls to set methods
608 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700609 public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 P.mPositiveButtonText = P.mContext.getText(textId);
611 P.mPositiveButtonListener = listener;
612 return this;
613 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 /**
616 * Set a listener to be invoked when the positive button of the dialog is pressed.
617 * @param text The text to display in the positive button
618 * @param listener The {@link DialogInterface.OnClickListener} to use.
619 *
620 * @return This Builder object to allow for chaining of calls to set methods
621 */
622 public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
623 P.mPositiveButtonText = text;
624 P.mPositiveButtonListener = listener;
625 return this;
626 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 /**
629 * Set a listener to be invoked when the negative button of the dialog is pressed.
630 * @param textId The resource id of the text to display in the negative button
631 * @param listener The {@link DialogInterface.OnClickListener} to use.
632 *
633 * @return This Builder object to allow for chaining of calls to set methods
634 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700635 public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 P.mNegativeButtonText = P.mContext.getText(textId);
637 P.mNegativeButtonListener = listener;
638 return this;
639 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 /**
642 * Set a listener to be invoked when the negative button of the dialog is pressed.
643 * @param text The text to display in the negative button
644 * @param listener The {@link DialogInterface.OnClickListener} to use.
645 *
646 * @return This Builder object to allow for chaining of calls to set methods
647 */
648 public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
649 P.mNegativeButtonText = text;
650 P.mNegativeButtonListener = listener;
651 return this;
652 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 /**
655 * Set a listener to be invoked when the neutral button of the dialog is pressed.
656 * @param textId The resource id of the text to display in the neutral button
657 * @param listener The {@link DialogInterface.OnClickListener} to use.
658 *
659 * @return This Builder object to allow for chaining of calls to set methods
660 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700661 public Builder setNeutralButton(@StringRes int textId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 P.mNeutralButtonText = P.mContext.getText(textId);
663 P.mNeutralButtonListener = listener;
664 return this;
665 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 /**
668 * Set a listener to be invoked when the neutral button of the dialog is pressed.
669 * @param text The text to display in the neutral button
670 * @param listener The {@link DialogInterface.OnClickListener} to use.
671 *
672 * @return This Builder object to allow for chaining of calls to set methods
673 */
674 public Builder setNeutralButton(CharSequence text, final OnClickListener listener) {
675 P.mNeutralButtonText = text;
676 P.mNeutralButtonListener = listener;
677 return this;
678 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 /**
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800681 * Sets whether the dialog is cancelable or not. Default is true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 *
683 * @return This Builder object to allow for chaining of calls to set methods
684 */
685 public Builder setCancelable(boolean cancelable) {
686 P.mCancelable = cancelable;
687 return this;
688 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 /**
691 * Sets the callback that will be called if the dialog is canceled.
Adam Powell7f02dc52012-08-27 13:35:16 -0700692 *
693 * <p>Even in a cancelable dialog, the dialog may be dismissed for reasons other than
694 * being canceled or one of the supplied choices being selected.
695 * If you are interested in listening for all cases where the dialog is dismissed
696 * and not just when it is canceled, see
697 * {@link #setOnDismissListener(android.content.DialogInterface.OnDismissListener) setOnDismissListener}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 * @see #setCancelable(boolean)
Adam Powell7f02dc52012-08-27 13:35:16 -0700699 * @see #setOnDismissListener(android.content.DialogInterface.OnDismissListener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 *
701 * @return This Builder object to allow for chaining of calls to set methods
702 */
703 public Builder setOnCancelListener(OnCancelListener onCancelListener) {
704 P.mOnCancelListener = onCancelListener;
705 return this;
706 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 /**
Adam Powell7f02dc52012-08-27 13:35:16 -0700709 * Sets the callback that will be called when the dialog is dismissed for any reason.
710 *
711 * @return This Builder object to allow for chaining of calls to set methods
712 */
713 public Builder setOnDismissListener(OnDismissListener onDismissListener) {
714 P.mOnDismissListener = onDismissListener;
715 return this;
716 }
717
718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 * Sets the callback that will be called if a key is dispatched to the dialog.
720 *
721 * @return This Builder object to allow for chaining of calls to set methods
722 */
723 public Builder setOnKeyListener(OnKeyListener onKeyListener) {
724 P.mOnKeyListener = onKeyListener;
725 return this;
726 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 /**
729 * Set a list of items to be displayed in the dialog as the content, you will be notified of the
730 * selected item via the supplied listener. This should be an array type i.e. R.array.foo
731 *
732 * @return This Builder object to allow for chaining of calls to set methods
733 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700734 public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 P.mItems = P.mContext.getResources().getTextArray(itemsId);
736 P.mOnClickListener = listener;
737 return this;
738 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 /**
741 * Set a list of items to be displayed in the dialog as the content, you will be notified of the
742 * selected item via the supplied listener.
743 *
744 * @return This Builder object to allow for chaining of calls to set methods
745 */
746 public Builder setItems(CharSequence[] items, final OnClickListener listener) {
747 P.mItems = items;
748 P.mOnClickListener = listener;
749 return this;
750 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 /**
753 * Set a list of items, which are supplied by the given {@link ListAdapter}, to be
754 * displayed in the dialog as the content, you will be notified of the
755 * selected item via the supplied listener.
Alan Viverette49f118e2015-02-23 17:15:27 -0800756 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 * @param adapter The {@link ListAdapter} to supply the list of items
758 * @param listener The listener that will be called when an item is clicked.
759 *
760 * @return This Builder object to allow for chaining of calls to set methods
761 */
762 public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) {
763 P.mAdapter = adapter;
764 P.mOnClickListener = listener;
765 return this;
766 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 /**
769 * Set a list of items, which are supplied by the given {@link Cursor}, to be
770 * displayed in the dialog as the content, you will be notified of the
771 * selected item via the supplied listener.
Alan Viverette49f118e2015-02-23 17:15:27 -0800772 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 * @param cursor The {@link Cursor} to supply the list of items
774 * @param listener The listener that will be called when an item is clicked.
775 * @param labelColumn The column name on the cursor containing the string to display
776 * in the label.
777 *
778 * @return This Builder object to allow for chaining of calls to set methods
779 */
780 public Builder setCursor(final Cursor cursor, final OnClickListener listener,
781 String labelColumn) {
782 P.mCursor = cursor;
783 P.mLabelColumn = labelColumn;
784 P.mOnClickListener = listener;
785 return this;
786 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 /**
789 * Set a list of items to be displayed in the dialog as the content,
790 * you will be notified of the selected item via the supplied listener.
791 * This should be an array type, e.g. R.array.foo. The list will have
792 * a check mark displayed to the right of the text for each checked
793 * item. Clicking on an item in the list will not dismiss the dialog.
794 * Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800795 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 * @param itemsId the resource id of an array i.e. R.array.foo
797 * @param checkedItems specifies which items are checked. It should be null in which case no
798 * items are checked. If non null it must be exactly the same length as the array of
799 * items.
800 * @param listener notified when an item on the list is clicked. The dialog will not be
801 * dismissed when an item is clicked. It will only be dismissed if clicked on a
802 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
803 *
804 * @return This Builder object to allow for chaining of calls to set methods
805 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700806 public Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 final OnMultiChoiceClickListener listener) {
808 P.mItems = P.mContext.getResources().getTextArray(itemsId);
809 P.mOnCheckboxClickListener = listener;
810 P.mCheckedItems = checkedItems;
811 P.mIsMultiChoice = true;
812 return this;
813 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 /**
816 * Set a list of items to be displayed in the dialog as the content,
817 * you will be notified of the selected item via the supplied listener.
818 * The list will have a check mark displayed to the right of the text
819 * for each checked item. Clicking on an item in the list will not
820 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800821 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 * @param items the text of the items to be displayed in the list.
823 * @param checkedItems specifies which items are checked. It should be null in which case no
824 * items are checked. If non null it must be exactly the same length as the array of
825 * items.
826 * @param listener notified when an item on the list is clicked. The dialog will not be
827 * dismissed when an item is clicked. It will only be dismissed if clicked on a
828 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
829 *
830 * @return This Builder object to allow for chaining of calls to set methods
831 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800832 public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 final OnMultiChoiceClickListener listener) {
834 P.mItems = items;
835 P.mOnCheckboxClickListener = listener;
836 P.mCheckedItems = checkedItems;
837 P.mIsMultiChoice = true;
838 return this;
839 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 /**
842 * Set a list of items to be displayed in the dialog as the content,
843 * you will be notified of the selected item via the supplied listener.
844 * The list will have a check mark displayed to the right of the text
845 * for each checked item. Clicking on an item in the list will not
846 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800847 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 * @param cursor the cursor used to provide the items.
849 * @param isCheckedColumn specifies the column name on the cursor to use to determine
850 * whether a checkbox is checked or not. It must return an integer value where 1
851 * means checked and 0 means unchecked.
852 * @param labelColumn The column name on the cursor containing the string to display in the
853 * label.
854 * @param listener notified when an item on the list is clicked. The dialog will not be
855 * dismissed when an item is clicked. It will only be dismissed if clicked on a
856 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
857 *
858 * @return This Builder object to allow for chaining of calls to set methods
859 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800860 public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 final OnMultiChoiceClickListener listener) {
862 P.mCursor = cursor;
863 P.mOnCheckboxClickListener = listener;
864 P.mIsCheckedColumn = isCheckedColumn;
865 P.mLabelColumn = labelColumn;
866 P.mIsMultiChoice = true;
867 return this;
868 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 /**
871 * Set a list of items to be displayed in the dialog as the content, you will be notified of
872 * the selected item via the supplied listener. This should be an array type i.e.
873 * R.array.foo The list will have a check mark displayed to the right of the text for the
874 * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a
875 * button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 * @param itemsId the resource id of an array i.e. R.array.foo
878 * @param checkedItem specifies which item is checked. If -1 no items are checked.
879 * @param listener notified when an item on the list is clicked. The dialog will not be
880 * dismissed when an item is clicked. It will only be dismissed if clicked on a
881 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
882 *
883 * @return This Builder object to allow for chaining of calls to set methods
884 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700885 public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 final OnClickListener listener) {
887 P.mItems = P.mContext.getResources().getTextArray(itemsId);
888 P.mOnClickListener = listener;
889 P.mCheckedItem = checkedItem;
890 P.mIsSingleChoice = true;
891 return this;
892 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 /**
895 * Set a list of items to be displayed in the dialog as the content, you will be notified of
896 * the selected item via the supplied listener. The list will have a check mark displayed to
897 * the right of the text for the checked item. Clicking on an item in the list will not
898 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800899 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 * @param cursor the cursor to retrieve the items from.
901 * @param checkedItem specifies which item is checked. If -1 no items are checked.
902 * @param labelColumn The column name on the cursor containing the string to display in the
903 * label.
904 * @param listener notified when an item on the list is clicked. The dialog will not be
905 * dismissed when an item is clicked. It will only be dismissed if clicked on a
906 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
907 *
908 * @return This Builder object to allow for chaining of calls to set methods
909 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800910 public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 final OnClickListener listener) {
912 P.mCursor = cursor;
913 P.mOnClickListener = listener;
914 P.mCheckedItem = checkedItem;
915 P.mLabelColumn = labelColumn;
916 P.mIsSingleChoice = true;
917 return this;
918 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 /**
921 * Set a list of items to be displayed in the dialog as the content, you will be notified of
922 * the selected item via the supplied listener. The list will have a check mark displayed to
923 * the right of the text for the checked item. Clicking on an item in the list will not
924 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800925 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 * @param items the items to be displayed.
927 * @param checkedItem specifies which item is checked. If -1 no items are checked.
928 * @param listener notified when an item on the list is clicked. The dialog will not be
929 * dismissed when an item is clicked. It will only be dismissed if clicked on a
930 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
931 *
932 * @return This Builder object to allow for chaining of calls to set methods
933 */
934 public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {
935 P.mItems = items;
936 P.mOnClickListener = listener;
937 P.mCheckedItem = checkedItem;
938 P.mIsSingleChoice = true;
939 return this;
Alan Viverette49f118e2015-02-23 17:15:27 -0800940 }
941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 /**
943 * Set a list of items to be displayed in the dialog as the content, you will be notified of
944 * the selected item via the supplied listener. The list will have a check mark displayed to
945 * the right of the text for the checked item. Clicking on an item in the list will not
946 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800947 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 * @param adapter The {@link ListAdapter} to supply the list of items
949 * @param checkedItem specifies which item is checked. If -1 no items are checked.
950 * @param listener notified when an item on the list is clicked. The dialog will not be
951 * dismissed when an item is clicked. It will only be dismissed if clicked on a
952 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
953 *
954 * @return This Builder object to allow for chaining of calls to set methods
955 */
956 public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) {
957 P.mAdapter = adapter;
958 P.mOnClickListener = listener;
959 P.mCheckedItem = checkedItem;
960 P.mIsSingleChoice = true;
961 return this;
962 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 /**
965 * Sets a listener to be invoked when an item in the list is selected.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 *
Alan Viverette49f118e2015-02-23 17:15:27 -0800967 * @param listener the listener to be invoked
968 * @return this Builder object to allow for chaining of calls to set methods
969 * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 */
971 public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {
972 P.mOnItemSelectedListener = listener;
973 return this;
974 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 /**
Alan Viveretteec186702013-12-05 11:10:31 -0800977 * Set a custom view resource to be the contents of the Dialog. The
978 * resource will be inflated, adding all top-level views to the screen.
979 *
980 * @param layoutResId Resource ID to be inflated.
Alan Viverette49f118e2015-02-23 17:15:27 -0800981 * @return this Builder object to allow for chaining of calls to set
Alan Viveretteec186702013-12-05 11:10:31 -0800982 * methods
983 */
984 public Builder setView(int layoutResId) {
985 P.mView = null;
986 P.mViewLayoutResId = layoutResId;
987 P.mViewSpacingSpecified = false;
988 return this;
989 }
990
991 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800992 * Sets a custom view to be the contents of the alert dialog.
993 * <p>
994 * When using a pre-Holo theme, if the supplied view is an instance of
995 * a {@link ListView} then the light background will be used.
996 * <p>
997 * <strong>Note:</strong> To ensure consistent styling, the custom view
998 * should be inflated or constructed using the alert dialog's themed
999 * context obtained via {@link #getContext()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 *
Alan Viverette49f118e2015-02-23 17:15:27 -08001001 * @param view the view to use as the contents of the alert dialog
1002 * @return this Builder object to allow for chaining of calls to set
1003 * methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 */
1005 public Builder setView(View view) {
1006 P.mView = view;
Alan Viveretteec186702013-12-05 11:10:31 -08001007 P.mViewLayoutResId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 P.mViewSpacingSpecified = false;
1009 return this;
1010 }
Alan Viverette49f118e2015-02-23 17:15:27 -08001011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001013 * Sets a custom view to be the contents of the alert dialog and
1014 * specifies additional padding around that view.
1015 * <p>
1016 * When using a pre-Holo theme, if the supplied view is an instance of
1017 * a {@link ListView} then the light background will be used.
1018 * <p>
1019 * <strong>Note:</strong> To ensure consistent styling, the custom view
1020 * should be inflated or constructed using the alert dialog's themed
1021 * context obtained via {@link #getContext()}.
1022 *
1023 * @param view the view to use as the contents of the alert dialog
1024 * @param viewSpacingLeft spacing between the left edge of the view and
1025 * the dialog frame
1026 * @param viewSpacingTop spacing between the top edge of the view and
1027 * the dialog frame
1028 * @param viewSpacingRight spacing between the right edge of the view
1029 * and the dialog frame
1030 * @param viewSpacingBottom spacing between the bottom edge of the view
1031 * and the dialog frame
1032 * @return this Builder object to allow for chaining of calls to set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 * methods
Alan Viverette49f118e2015-02-23 17:15:27 -08001034 *
1035 * @hide Remove once the framework usages have been replaced.
1036 * @deprecated Set the padding on the view itself.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
Alan Viverette49f118e2015-02-23 17:15:27 -08001038 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
1040 int viewSpacingRight, int viewSpacingBottom) {
1041 P.mView = view;
Alan Viveretteec186702013-12-05 11:10:31 -08001042 P.mViewLayoutResId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 P.mViewSpacingSpecified = true;
1044 P.mViewSpacingLeft = viewSpacingLeft;
1045 P.mViewSpacingTop = viewSpacingTop;
1046 P.mViewSpacingRight = viewSpacingRight;
1047 P.mViewSpacingBottom = viewSpacingBottom;
1048 return this;
1049 }
Alan Viverette49f118e2015-02-23 17:15:27 -08001050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001052 * Sets the alert dialog to use the inverse background, regardless of
1053 * what the contents is.
1054 *
1055 * @param useInverseBackground whether to use the inverse background
1056 * @return this Builder object to allow for chaining of calls to set methods
1057 * @deprecated This flag is only used for pre-Material themes. Instead,
1058 * specify the window background using on the alert dialog
1059 * theme.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 */
Alan Viverette49f118e2015-02-23 17:15:27 -08001061 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 public Builder setInverseBackgroundForced(boolean useInverseBackground) {
1063 P.mForceInverseBackground = useInverseBackground;
1064 return this;
1065 }
Romain Guy9c802c12009-03-25 15:07:31 -07001066
1067 /**
1068 * @hide
1069 */
1070 public Builder setRecycleOnMeasureEnabled(boolean enabled) {
1071 P.mRecycleOnMeasure = enabled;
1072 return this;
1073 }
1074
1075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001077 * Creates an {@link AlertDialog} with the arguments supplied to this
1078 * builder.
1079 * <p>
1080 * Calling this method does not display the dialog. If no additional
1081 * processing is needed, {@link #show()} may be called instead to both
1082 * create and display the dialog.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 */
1084 public AlertDialog create() {
Alan Viverette93d9e262015-08-11 15:02:42 -04001085 // Context has already been wrapped with the appropriate theme.
1086 final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 P.apply(dialog.mAlert);
Adam Powellfbca7692011-06-22 21:29:19 -07001088 dialog.setCancelable(P.mCancelable);
1089 if (P.mCancelable) {
1090 dialog.setCanceledOnTouchOutside(true);
1091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 dialog.setOnCancelListener(P.mOnCancelListener);
Adam Powell7f02dc52012-08-27 13:35:16 -07001093 dialog.setOnDismissListener(P.mOnDismissListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 if (P.mOnKeyListener != null) {
1095 dialog.setOnKeyListener(P.mOnKeyListener);
1096 }
1097 return dialog;
1098 }
1099
1100 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001101 * Creates an {@link AlertDialog} with the arguments supplied to this
1102 * builder and immediately displays the dialog.
1103 * <p>
1104 * Calling this method is functionally identical to:
1105 * <pre>
1106 * AlertDialog dialog = builder.create();
1107 * dialog.show();
1108 * </pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 */
1110 public AlertDialog show() {
Alan Viverette49f118e2015-02-23 17:15:27 -08001111 final AlertDialog dialog = create();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 dialog.show();
1113 return dialog;
1114 }
1115 }
Alan Viverette49f118e2015-02-23 17:15:27 -08001116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117}