blob: 07b4b9c39e1e9ec12d6ae7c2280e61a332032a96 [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
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.ArrayRes;
20import android.annotation.AttrRes;
21import android.annotation.DrawableRes;
22import android.annotation.StringRes;
Alan Viverette682a4332015-04-10 11:05:50 -070023import android.annotation.StyleRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
25import android.content.DialogInterface;
Adam Lesinski9553fb32017-05-23 18:53:44 -070026import android.content.res.ResourceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.database.Cursor;
28import android.graphics.drawable.Drawable;
29import android.os.Bundle;
30import android.os.Message;
Makoto Onuki778ce662018-04-20 14:04:50 -070031import android.text.Layout;
32import android.text.method.MovementMethod;
Adam Powell2fbf4de62010-09-30 15:46:46 -070033import android.util.TypedValue;
Adam Powellc49c1732010-09-28 16:03:15 -070034import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.view.KeyEvent;
36import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import 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;
Makoto Onuki778ce662018-04-20 14:04:50 -070043import com.android.internal.app.AlertController;
Alan Viverette49f118e2015-02-23 17:15:27 -080044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045/**
46 * A subclass of Dialog that can display one, two or three buttons. If you only want to
47 * display a String in this dialog box, use the setMessage() method. If you
Adam Powellc9006872010-03-18 13:44:45 -070048 * want to display a more complex view, look up the FrameLayout called "custom"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * and add your view to it:
50 *
51 * <pre>
Alan Viverette51efddb2017-04-05 10:00:01 -040052 * FrameLayout fl = findViewById(android.R.id.custom);
Adam Powellc9006872010-03-18 13:44:45 -070053 * fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 * </pre>
Alan Viverette49f118e2015-02-23 17:15:27 -080055 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 * <p>The AlertDialog class takes care of automatically setting
Makoto Onuki778ce662018-04-20 14:04:50 -070057 * {@link android.view.WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 * WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} for you based on whether
59 * any views in the dialog return true from {@link View#onCheckIsTextEditor()
60 * View.onCheckIsTextEditor()}. Generally you want this set for a Dialog
61 * without text editors, so that it will be placed on top of the current
62 * input method UI. You can modify this behavior by forcing the flag to your
63 * desired mode after calling {@link #onCreate}.
Joe Fernandez558459f2011-10-13 16:47:36 -070064 *
65 * <div class="special reference">
66 * <h3>Developer Guides</h3>
67 * <p>For more information about creating dialogs, read the
68 * <a href="{@docRoot}guide/topics/ui/dialogs.html">Dialogs</a> developer guide.</p>
69 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 */
71public class AlertDialog extends Dialog implements DialogInterface {
72 private AlertController mAlert;
73
Dianne Hackborne79b5542011-01-27 15:18:46 -080074 /**
75 * Special theme constant for {@link #AlertDialog(Context, int)}: use
76 * the traditional (pre-Holo) alert dialog theme.
Alan Viverette49f118e2015-02-23 17:15:27 -080077 *
78 * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
Dianne Hackborne79b5542011-01-27 15:18:46 -080079 */
Alan Viverette49f118e2015-02-23 17:15:27 -080080 @Deprecated
Dianne Hackborne79b5542011-01-27 15:18:46 -080081 public static final int THEME_TRADITIONAL = 1;
Alan Viverette49f118e2015-02-23 17:15:27 -080082
Dianne Hackborne79b5542011-01-27 15:18:46 -080083 /**
84 * Special theme constant for {@link #AlertDialog(Context, int)}: use
85 * the holographic alert theme with a dark background.
Alan Viverette49f118e2015-02-23 17:15:27 -080086 *
87 * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
Dianne Hackborne79b5542011-01-27 15:18:46 -080088 */
Alan Viverette49f118e2015-02-23 17:15:27 -080089 @Deprecated
Dianne Hackborne79b5542011-01-27 15:18:46 -080090 public static final int THEME_HOLO_DARK = 2;
Alan Viverette49f118e2015-02-23 17:15:27 -080091
Dianne Hackborne79b5542011-01-27 15:18:46 -080092 /**
93 * Special theme constant for {@link #AlertDialog(Context, int)}: use
94 * the holographic alert theme with a light background.
Alan Viverette49f118e2015-02-23 17:15:27 -080095 *
96 * @deprecated Use {@link android.R.style#Theme_Material_Light_Dialog_Alert}.
Dianne Hackborne79b5542011-01-27 15:18:46 -080097 */
Alan Viverette49f118e2015-02-23 17:15:27 -080098 @Deprecated
Dianne Hackborne79b5542011-01-27 15:18:46 -080099 public static final int THEME_HOLO_LIGHT = 3;
Adam Powell6e90a362011-08-14 16:48:32 -0700100
101 /**
102 * Special theme constant for {@link #AlertDialog(Context, int)}: use
103 * the device's default alert theme with a dark background.
Alan Viverette49f118e2015-02-23 17:15:27 -0800104 *
105 * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Dialog_Alert}.
Adam Powell6e90a362011-08-14 16:48:32 -0700106 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800107 @Deprecated
Adam Powell6e90a362011-08-14 16:48:32 -0700108 public static final int THEME_DEVICE_DEFAULT_DARK = 4;
109
110 /**
111 * Special theme constant for {@link #AlertDialog(Context, int)}: use
Scott Kennedy7ed189e2013-01-11 22:31:43 -0800112 * the device's default alert theme with a light background.
Alan Viverette49f118e2015-02-23 17:15:27 -0800113 *
114 * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Light_Dialog_Alert}.
Adam Powell6e90a362011-08-14 16:48:32 -0700115 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800116 @Deprecated
Adam Powell6e90a362011-08-14 16:48:32 -0700117 public static final int THEME_DEVICE_DEFAULT_LIGHT = 5;
Craig Stout4c0cb8a2014-04-04 13:03:10 -0700118
119 /**
120 * No layout hint.
121 * @hide
122 */
123 public static final int LAYOUT_HINT_NONE = 0;
124
125 /**
126 * Hint layout to the side.
127 * @hide
128 */
129 public static final int LAYOUT_HINT_SIDE = 1;
Alan Viverette49f118e2015-02-23 17:15:27 -0800130
131 /**
132 * Creates an alert dialog that uses the default alert dialog theme.
133 * <p>
134 * The default alert dialog theme is defined by
135 * {@link android.R.attr#alertDialogTheme} within the parent
136 * {@code context}'s theme.
137 *
138 * @param context the parent context
Alan Viverette5696f042015-04-08 11:03:32 -0700139 * @see android.R.styleable#Theme_alertDialogTheme
Alan Viverette49f118e2015-02-23 17:15:27 -0800140 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 protected AlertDialog(Context context) {
Alan Viverette49f118e2015-02-23 17:15:27 -0800142 this(context, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 }
144
Dianne Hackborne79b5542011-01-27 15:18:46 -0800145 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800146 * Creates an alert dialog that uses the default alert dialog theme and a
147 * custom cancel listener.
148 * <p>
149 * This is functionally identical to:
150 * <pre>
151 * AlertDialog dialog = new AlertDialog(context);
152 * alertDialog.setCancelable(cancelable);
153 * alertDialog.setOnCancelListener(cancelListener);
154 * </pre>
155 * <p>
156 * The default alert dialog theme is defined by
157 * {@link android.R.attr#alertDialogTheme} within the parent
158 * {@code context}'s theme.
159 *
160 * @param context the parent context
Alan Viverette5696f042015-04-08 11:03:32 -0700161 * @see android.R.styleable#Theme_alertDialogTheme
Dianne Hackborne79b5542011-01-27 15:18:46 -0800162 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800163 protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
164 this(context, 0);
165
166 setCancelable(cancelable);
167 setOnCancelListener(cancelListener);
Dianne Hackborne79b5542011-01-27 15:18:46 -0800168 }
169
Alan Viverette49f118e2015-02-23 17:15:27 -0800170 /**
171 * Creates an alert dialog that uses an explicit theme resource.
172 * <p>
173 * The specified theme resource ({@code themeResId}) is applied on top of
174 * the parent {@code context}'s theme. It may be specified as a style
175 * resource containing a fully-populated theme, such as
176 * {@link android.R.style#Theme_Material_Dialog}, to replace all attributes
177 * in the parent {@code context}'s theme including primary and accent
178 * colors.
179 * <p>
180 * To preserve attributes such as primary and accent colors, the
181 * {@code themeResId} may instead be specified as an overlay theme such as
182 * {@link android.R.style#ThemeOverlay_Material_Dialog}. This will override
183 * only the window attributes necessary to style the alert window as a
184 * dialog.
185 * <p>
186 * Alternatively, the {@code themeResId} may be specified as {@code 0} to
187 * use the parent {@code context}'s resolved value for
188 * {@link android.R.attr#alertDialogTheme}.
189 *
190 * @param context the parent context
191 * @param themeResId the resource ID of the theme against which to inflate
192 * this dialog, or {@code 0} to use the parent
193 * {@code context}'s default alert dialog theme
Alan Viverette5696f042015-04-08 11:03:32 -0700194 * @see android.R.styleable#Theme_alertDialogTheme
Alan Viverette49f118e2015-02-23 17:15:27 -0800195 */
Alan Viverette682a4332015-04-10 11:05:50 -0700196 protected AlertDialog(Context context, @StyleRes int themeResId) {
Alan Viverette5696f042015-04-08 11:03:32 -0700197 this(context, themeResId, true);
198 }
199
Alan Viverette682a4332015-04-10 11:05:50 -0700200 AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
Alan Viverette5696f042015-04-08 11:03:32 -0700201 super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0,
202 createContextThemeWrapper);
Jeff Browna492c3a2012-08-23 19:48:44 -0700203
Dianne Hackborncfaf8872011-01-18 13:57:54 -0800204 mWindow.alwaysReadCloseOnTouchAttr();
Michael Kwancc6e6f02016-05-16 12:54:57 -0700205 mAlert = AlertController.create(getContext(), this, getWindow());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 }
207
Adam Lesinski9553fb32017-05-23 18:53:44 -0700208 static @StyleRes int resolveDialogTheme(Context context, @StyleRes int themeResId) {
Alan Viverette682a4332015-04-10 11:05:50 -0700209 if (themeResId == THEME_TRADITIONAL) {
Alan Viverette49f118e2015-02-23 17:15:27 -0800210 return R.style.Theme_Dialog_Alert;
211 } else if (themeResId == THEME_HOLO_DARK) {
212 return R.style.Theme_Holo_Dialog_Alert;
213 } else if (themeResId == THEME_HOLO_LIGHT) {
214 return R.style.Theme_Holo_Light_Dialog_Alert;
215 } else if (themeResId == THEME_DEVICE_DEFAULT_DARK) {
216 return R.style.Theme_DeviceDefault_Dialog_Alert;
217 } else if (themeResId == THEME_DEVICE_DEFAULT_LIGHT) {
218 return R.style.Theme_DeviceDefault_Light_Dialog_Alert;
Adam Lesinski9553fb32017-05-23 18:53:44 -0700219 } else if (ResourceId.isValid(themeResId)) {
Adam Lesinski36018212017-04-27 18:09:19 -0700220 // start of real resource IDs.
Alan Viverette49f118e2015-02-23 17:15:27 -0800221 return themeResId;
Dianne Hackborne79b5542011-01-27 15:18:46 -0800222 } else {
Alan Viverette49f118e2015-02-23 17:15:27 -0800223 final TypedValue outValue = new TypedValue();
224 context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true);
Dianne Hackborne79b5542011-01-27 15:18:46 -0800225 return outValue.resourceId;
226 }
Adam Powell2fbf4de62010-09-30 15:46:46 -0700227 }
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /**
Alan Viverettea1e63312013-12-09 18:20:19 -0800230 * Gets one of the buttons used in the dialog. Returns null if the specified
Alan Viverettef34ef742013-12-11 15:59:53 -0800231 * button does not exist or the dialog has not yet been fully created (for
232 * example, via {@link #show()} or {@link #create()}).
Alan Viverettea1e63312013-12-09 18:20:19 -0800233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * @param whichButton The identifier of the button that should be returned.
235 * For example, this can be
236 * {@link DialogInterface#BUTTON_POSITIVE}.
237 * @return The button from the dialog, or null if a button does not exist.
238 */
239 public Button getButton(int whichButton) {
240 return mAlert.getButton(whichButton);
241 }
Alan Viverettea1e63312013-12-09 18:20:19 -0800242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 /**
244 * Gets the list view used in the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 * @return The {@link ListView} from the dialog.
247 */
248 public ListView getListView() {
249 return mAlert.getListView();
250 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 @Override
253 public void setTitle(CharSequence title) {
254 super.setTitle(title);
255 mAlert.setTitle(title);
256 }
257
258 /**
259 * @see Builder#setCustomTitle(View)
260 */
261 public void setCustomTitle(View customTitleView) {
262 mAlert.setCustomTitle(customTitleView);
263 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 public void setMessage(CharSequence message) {
266 mAlert.setMessage(message);
267 }
268
Makoto Onuki778ce662018-04-20 14:04:50 -0700269 /** @hide */
270 public void setMessageMovementMethod(MovementMethod movementMethod) {
271 mAlert.setMessageMovementMethod(movementMethod);
272 }
273
274 /** @hide */
275 public void setMessageHyphenationFrequency(
276 @Layout.HyphenationFrequency int hyphenationFrequency) {
277 mAlert.setMessageHyphenationFrequency(hyphenationFrequency);
278 }
279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * Set the view to display in that dialog.
282 */
283 public void setView(View view) {
284 mAlert.setView(view);
285 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800288 * 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 -0800289 * view.
290 *
291 * @param view The view to show in the content area of the dialog
292 * @param viewSpacingLeft Extra space to appear to the left of {@code view}
293 * @param viewSpacingTop Extra space to appear above {@code view}
294 * @param viewSpacingRight Extra space to appear to the right of {@code view}
295 * @param viewSpacingBottom Extra space to appear below {@code view}
296 */
297 public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,
298 int viewSpacingBottom) {
299 mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);
300 }
301
302 /**
Craig Stout4c0cb8a2014-04-04 13:03:10 -0700303 * Internal api to allow hinting for the best button panel layout.
304 * @hide
305 */
306 void setButtonPanelLayoutHint(int layoutHint) {
307 mAlert.setButtonPanelLayoutHint(layoutHint);
308 }
309
310 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 * Set a message to be sent when a button is pressed.
Alan Viverette49f118e2015-02-23 17:15:27 -0800312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * @param whichButton Which button to set the message for, can be one of
314 * {@link DialogInterface#BUTTON_POSITIVE},
315 * {@link DialogInterface#BUTTON_NEGATIVE}, or
316 * {@link DialogInterface#BUTTON_NEUTRAL}
317 * @param text The text to display in positive button.
318 * @param msg The {@link Message} to be sent when clicked.
319 */
320 public void setButton(int whichButton, CharSequence text, Message msg) {
321 mAlert.setButton(whichButton, text, null, msg);
322 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * Set a listener to be invoked when the positive button of the dialog is pressed.
Alan Viverette49f118e2015-02-23 17:15:27 -0800326 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 * @param whichButton Which button to set the listener on, can be one of
328 * {@link DialogInterface#BUTTON_POSITIVE},
329 * {@link DialogInterface#BUTTON_NEGATIVE}, or
330 * {@link DialogInterface#BUTTON_NEUTRAL}
331 * @param text The text to display in positive button.
332 * @param listener The {@link DialogInterface.OnClickListener} to use.
333 */
334 public void setButton(int whichButton, CharSequence text, OnClickListener listener) {
335 mAlert.setButton(whichButton, text, listener, null);
336 }
337
338 /**
339 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
340 * {@link DialogInterface#BUTTON_POSITIVE}.
341 */
342 @Deprecated
343 public void setButton(CharSequence text, Message msg) {
344 setButton(BUTTON_POSITIVE, text, msg);
345 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
348 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
349 * {@link DialogInterface#BUTTON_NEGATIVE}.
350 */
351 @Deprecated
352 public void setButton2(CharSequence text, Message msg) {
353 setButton(BUTTON_NEGATIVE, text, msg);
354 }
355
356 /**
357 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
358 * {@link DialogInterface#BUTTON_NEUTRAL}.
359 */
360 @Deprecated
361 public void setButton3(CharSequence text, Message msg) {
362 setButton(BUTTON_NEUTRAL, text, msg);
363 }
364
365 /**
366 * Set a listener to be invoked when button 1 of the dialog is pressed.
Alan Viverette49f118e2015-02-23 17:15:27 -0800367 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 * @param text The text to display in button 1.
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_POSITIVE}
373 */
374 @Deprecated
375 public void setButton(CharSequence text, final OnClickListener listener) {
376 setButton(BUTTON_POSITIVE, text, listener);
377 }
378
379 /**
380 * Set a listener to be invoked when button 2 of the dialog is pressed.
381 * @param text The text to display in button 2.
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_NEGATIVE}
386 */
387 @Deprecated
388 public void setButton2(CharSequence text, final OnClickListener listener) {
389 setButton(BUTTON_NEGATIVE, text, listener);
390 }
391
392 /**
393 * Set a listener to be invoked when button 3 of the dialog is pressed.
394 * @param text The text to display in button 3.
395 * @param listener The {@link DialogInterface.OnClickListener} to use.
396 * @deprecated Use
397 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
398 * with {@link DialogInterface#BUTTON_POSITIVE}
399 */
400 @Deprecated
401 public void setButton3(CharSequence text, final OnClickListener listener) {
402 setButton(BUTTON_NEUTRAL, text, listener);
403 }
404
405 /**
406 * Set resId to 0 if you don't want an icon.
407 * @param resId the resourceId of the drawable to use as the icon or 0
408 * if you don't want an icon.
409 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700410 public void setIcon(@DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 mAlert.setIcon(resId);
412 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 public void setIcon(Drawable icon) {
415 mAlert.setIcon(icon);
416 }
417
Adam Powell947f7822011-01-07 22:30:48 -0800418 /**
419 * Set an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon
420 *
421 * @param attrId ID of a theme attribute that points to a drawable resource.
422 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700423 public void setIconAttribute(@AttrRes int attrId) {
Adam Powell947f7822011-01-07 22:30:48 -0800424 TypedValue out = new TypedValue();
425 mContext.getTheme().resolveAttribute(attrId, out, true);
426 mAlert.setIcon(out.resourceId);
427 }
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 public void setInverseBackgroundForced(boolean forceInverseBackground) {
430 mAlert.setInverseBackgroundForced(forceInverseBackground);
431 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 @Override
434 protected void onCreate(Bundle savedInstanceState) {
435 super.onCreate(savedInstanceState);
436 mAlert.installContent();
437 }
438
439 @Override
440 public boolean onKeyDown(int keyCode, KeyEvent event) {
441 if (mAlert.onKeyDown(keyCode, event)) return true;
442 return super.onKeyDown(keyCode, event);
443 }
444
445 @Override
446 public boolean onKeyUp(int keyCode, KeyEvent event) {
447 if (mAlert.onKeyUp(keyCode, event)) return true;
448 return super.onKeyUp(keyCode, event);
449 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 public static class Builder {
452 private final AlertController.AlertParams P;
Alan Viverette49f118e2015-02-23 17:15:27 -0800453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800455 * Creates a builder for an alert dialog that uses the default alert
456 * dialog theme.
457 * <p>
458 * The default alert dialog theme is defined by
459 * {@link android.R.attr#alertDialogTheme} within the parent
460 * {@code context}'s theme.
461 *
462 * @param context the parent context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 */
464 public Builder(Context context) {
Adam Lesinski9553fb32017-05-23 18:53:44 -0700465 this(context, resolveDialogTheme(context, ResourceId.ID_NULL));
Martin Nordholtse429c5e2010-06-15 16:17:24 +0200466 }
467
468 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800469 * Creates a builder for an alert dialog that uses an explicit theme
470 * resource.
471 * <p>
472 * The specified theme resource ({@code themeResId}) is applied on top
473 * of the parent {@code context}'s theme. It may be specified as a
474 * style resource containing a fully-populated theme, such as
475 * {@link android.R.style#Theme_Material_Dialog}, to replace all
476 * attributes in the parent {@code context}'s theme including primary
477 * and accent colors.
478 * <p>
479 * To preserve attributes such as primary and accent colors, the
480 * {@code themeResId} may instead be specified as an overlay theme such
481 * as {@link android.R.style#ThemeOverlay_Material_Dialog}. This will
482 * override only the window attributes necessary to style the alert
483 * window as a dialog.
484 * <p>
485 * Alternatively, the {@code themeResId} may be specified as {@code 0}
486 * to use the parent {@code context}'s resolved value for
487 * {@link android.R.attr#alertDialogTheme}.
488 *
489 * @param context the parent context
490 * @param themeResId the resource ID of the theme against which to inflate
491 * this dialog, or {@code 0} to use the parent
492 * {@code context}'s default alert dialog theme
Martin Nordholtse429c5e2010-06-15 16:17:24 +0200493 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800494 public Builder(Context context, int themeResId) {
Dianne Hackborne79b5542011-01-27 15:18:46 -0800495 P = new AlertController.AlertParams(new ContextThemeWrapper(
Alan Viverette49f118e2015-02-23 17:15:27 -0800496 context, resolveDialogTheme(context, themeResId)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 /**
Adam Powellc49c1732010-09-28 16:03:15 -0700500 * Returns a {@link Context} with the appropriate theme for dialogs created by this Builder.
501 * Applications should use this Context for obtaining LayoutInflaters for inflating views
502 * that will be used in the resulting dialogs, as it will cause views to be inflated with
503 * the correct theme.
504 *
505 * @return A Context for built Dialogs.
506 */
507 public Context getContext() {
Adam Powellccb013f2010-10-12 18:42:56 -0700508 return P.mContext;
Adam Powellc49c1732010-09-28 16:03:15 -0700509 }
510
511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 * Set the title using the given resource id.
513 *
514 * @return This Builder object to allow for chaining of calls to set methods
515 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700516 public Builder setTitle(@StringRes int titleId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 P.mTitle = P.mContext.getText(titleId);
518 return this;
519 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 /**
522 * Set the title displayed in the {@link Dialog}.
523 *
524 * @return This Builder object to allow for chaining of calls to set methods
525 */
526 public Builder setTitle(CharSequence title) {
527 P.mTitle = title;
528 return this;
529 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800532 * Set the title using the custom view {@code customTitleView}.
533 * <p>
534 * The methods {@link #setTitle(int)} and {@link #setIcon(int)} should
535 * be sufficient for most titles, but this is provided if the title
536 * needs more customization. Using this will replace the title and icon
537 * set via the other methods.
538 * <p>
539 * <strong>Note:</strong> To ensure consistent styling, the custom view
540 * should be inflated or constructed using the alert dialog's themed
541 * context obtained via {@link #getContext()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 *
Alan Viverette49f118e2015-02-23 17:15:27 -0800543 * @param customTitleView the custom view to use as the title
544 * @return this Builder object to allow for chaining of calls to set
545 * methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 */
547 public Builder setCustomTitle(View customTitleView) {
548 P.mCustomTitleView = customTitleView;
549 return this;
550 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 /**
553 * Set the message to display using the given resource id.
554 *
555 * @return This Builder object to allow for chaining of calls to set methods
556 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700557 public Builder setMessage(@StringRes int messageId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 P.mMessage = P.mContext.getText(messageId);
559 return this;
560 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 /**
563 * Set the message to display.
564 *
565 * @return This Builder object to allow for chaining of calls to set methods
566 */
567 public Builder setMessage(CharSequence message) {
568 P.mMessage = message;
569 return this;
570 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 /**
573 * Set the resource id of the {@link Drawable} to be used in the title.
Alan Viverette5c2d8f72015-01-05 12:56:45 -0800574 * <p>
575 * Takes precedence over values set using {@link #setIcon(Drawable)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 *
577 * @return This Builder object to allow for chaining of calls to set methods
578 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700579 public Builder setIcon(@DrawableRes int iconId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 P.mIconId = iconId;
581 return this;
582 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 /**
585 * Set the {@link Drawable} to be used in the title.
Alan Viverette49f118e2015-02-23 17:15:27 -0800586 * <p>
587 * <strong>Note:</strong> To ensure consistent styling, the drawable
588 * should be inflated or constructed using the alert dialog's themed
589 * context obtained via {@link #getContext()}.
590 *
591 * @return this Builder object to allow for chaining of calls to set
592 * methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 */
594 public Builder setIcon(Drawable icon) {
595 P.mIcon = icon;
596 return this;
597 }
Adam Powell947f7822011-01-07 22:30:48 -0800598
599 /**
Alan Viverette5c2d8f72015-01-05 12:56:45 -0800600 * Set an icon as supplied by a theme attribute. e.g.
601 * {@link android.R.attr#alertDialogIcon}.
602 * <p>
603 * Takes precedence over values set using {@link #setIcon(int)} or
604 * {@link #setIcon(Drawable)}.
Adam Powell947f7822011-01-07 22:30:48 -0800605 *
606 * @param attrId ID of a theme attribute that points to a drawable resource.
607 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700608 public Builder setIconAttribute(@AttrRes int attrId) {
Adam Powell947f7822011-01-07 22:30:48 -0800609 TypedValue out = new TypedValue();
610 P.mContext.getTheme().resolveAttribute(attrId, out, true);
611 P.mIconId = out.resourceId;
612 return this;
613 }
614
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 textId The resource id of 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 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700622 public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 P.mPositiveButtonText = P.mContext.getText(textId);
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 positive button of the dialog is pressed.
630 * @param text The text to display in the positive 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 */
635 public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
636 P.mPositiveButtonText = text;
637 P.mPositiveButtonListener = 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 textId The resource id of 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 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700648 public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 P.mNegativeButtonText = P.mContext.getText(textId);
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 negative button of the dialog is pressed.
656 * @param text The text to display in the negative 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 */
661 public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
662 P.mNegativeButtonText = text;
663 P.mNegativeButtonListener = 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 textId The resource id of 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 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700674 public Builder setNeutralButton(@StringRes int textId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 P.mNeutralButtonText = P.mContext.getText(textId);
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 /**
681 * Set a listener to be invoked when the neutral button of the dialog is pressed.
682 * @param text The text to display in the neutral button
683 * @param listener The {@link DialogInterface.OnClickListener} to use.
684 *
685 * @return This Builder object to allow for chaining of calls to set methods
686 */
687 public Builder setNeutralButton(CharSequence text, final OnClickListener listener) {
688 P.mNeutralButtonText = text;
689 P.mNeutralButtonListener = listener;
690 return this;
691 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 /**
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800694 * Sets whether the dialog is cancelable or not. Default is true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 *
696 * @return This Builder object to allow for chaining of calls to set methods
697 */
698 public Builder setCancelable(boolean cancelable) {
699 P.mCancelable = cancelable;
700 return this;
701 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 /**
704 * Sets the callback that will be called if the dialog is canceled.
Adam Powell7f02dc52012-08-27 13:35:16 -0700705 *
706 * <p>Even in a cancelable dialog, the dialog may be dismissed for reasons other than
707 * being canceled or one of the supplied choices being selected.
708 * If you are interested in listening for all cases where the dialog is dismissed
709 * and not just when it is canceled, see
710 * {@link #setOnDismissListener(android.content.DialogInterface.OnDismissListener) setOnDismissListener}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 * @see #setCancelable(boolean)
Adam Powell7f02dc52012-08-27 13:35:16 -0700712 * @see #setOnDismissListener(android.content.DialogInterface.OnDismissListener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 *
714 * @return This Builder object to allow for chaining of calls to set methods
715 */
716 public Builder setOnCancelListener(OnCancelListener onCancelListener) {
717 P.mOnCancelListener = onCancelListener;
718 return this;
719 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 /**
Adam Powell7f02dc52012-08-27 13:35:16 -0700722 * Sets the callback that will be called when the dialog is dismissed for any reason.
723 *
724 * @return This Builder object to allow for chaining of calls to set methods
725 */
726 public Builder setOnDismissListener(OnDismissListener onDismissListener) {
727 P.mOnDismissListener = onDismissListener;
728 return this;
729 }
730
731 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 * Sets the callback that will be called if a key is dispatched to the dialog.
733 *
734 * @return This Builder object to allow for chaining of calls to set methods
735 */
736 public Builder setOnKeyListener(OnKeyListener onKeyListener) {
737 P.mOnKeyListener = onKeyListener;
738 return this;
739 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 /**
742 * Set a list of items to be displayed in the dialog as the content, you will be notified of the
743 * selected item via the supplied listener. This should be an array type i.e. R.array.foo
744 *
745 * @return This Builder object to allow for chaining of calls to set methods
746 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700747 public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 P.mItems = P.mContext.getResources().getTextArray(itemsId);
749 P.mOnClickListener = listener;
750 return this;
751 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 /**
754 * Set a list of items to be displayed in the dialog as the content, you will be notified of the
755 * selected item via the supplied listener.
756 *
757 * @return This Builder object to allow for chaining of calls to set methods
758 */
759 public Builder setItems(CharSequence[] items, final OnClickListener listener) {
760 P.mItems = items;
761 P.mOnClickListener = listener;
762 return this;
763 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 /**
766 * Set a list of items, which are supplied by the given {@link ListAdapter}, to be
767 * displayed in the dialog as the content, you will be notified of the
768 * selected item via the supplied listener.
Alan Viverette49f118e2015-02-23 17:15:27 -0800769 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 * @param adapter The {@link ListAdapter} to supply the list of items
771 * @param listener The listener that will be called when an item is clicked.
772 *
773 * @return This Builder object to allow for chaining of calls to set methods
774 */
775 public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) {
776 P.mAdapter = adapter;
777 P.mOnClickListener = listener;
778 return this;
779 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 /**
782 * Set a list of items, which are supplied by the given {@link Cursor}, to be
783 * displayed in the dialog as the content, you will be notified of the
784 * selected item via the supplied listener.
Alan Viverette49f118e2015-02-23 17:15:27 -0800785 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 * @param cursor The {@link Cursor} to supply the list of items
787 * @param listener The listener that will be called when an item is clicked.
788 * @param labelColumn The column name on the cursor containing the string to display
789 * in the label.
790 *
791 * @return This Builder object to allow for chaining of calls to set methods
792 */
793 public Builder setCursor(final Cursor cursor, final OnClickListener listener,
794 String labelColumn) {
795 P.mCursor = cursor;
796 P.mLabelColumn = labelColumn;
797 P.mOnClickListener = listener;
798 return this;
799 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 /**
802 * Set a list of items to be displayed in the dialog as the content,
803 * you will be notified of the selected item via the supplied listener.
804 * This should be an array type, e.g. R.array.foo. The list will have
805 * a check mark displayed to the right of the text for each checked
806 * item. Clicking on an item in the list will not dismiss the dialog.
807 * Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800808 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 * @param itemsId the resource id of an array i.e. R.array.foo
810 * @param checkedItems specifies which items are checked. It should be null in which case no
811 * items are checked. If non null it must be exactly the same length as the array of
812 * items.
813 * @param listener notified when an item on the list is clicked. The dialog will not be
814 * dismissed when an item is clicked. It will only be dismissed if clicked on a
815 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
816 *
817 * @return This Builder object to allow for chaining of calls to set methods
818 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700819 public Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 final OnMultiChoiceClickListener listener) {
821 P.mItems = P.mContext.getResources().getTextArray(itemsId);
822 P.mOnCheckboxClickListener = listener;
823 P.mCheckedItems = checkedItems;
824 P.mIsMultiChoice = true;
825 return this;
826 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 /**
829 * Set a list of items to be displayed in the dialog as the content,
830 * you will be notified of the selected item via the supplied listener.
831 * The list will have a check mark displayed to the right of the text
832 * for each checked item. Clicking on an item in the list will not
833 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800834 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 * @param items the text of the items to be displayed in the list.
836 * @param checkedItems specifies which items are checked. It should be null in which case no
837 * items are checked. If non null it must be exactly the same length as the array of
838 * items.
839 * @param listener notified when an item on the list is clicked. The dialog will not be
840 * dismissed when an item is clicked. It will only be dismissed if clicked on a
841 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
842 *
843 * @return This Builder object to allow for chaining of calls to set methods
844 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800845 public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 final OnMultiChoiceClickListener listener) {
847 P.mItems = items;
848 P.mOnCheckboxClickListener = listener;
849 P.mCheckedItems = checkedItems;
850 P.mIsMultiChoice = true;
851 return this;
852 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 /**
855 * Set a list of items to be displayed in the dialog as the content,
856 * you will be notified of the selected item via the supplied listener.
857 * The list will have a check mark displayed to the right of the text
858 * for each checked item. Clicking on an item in the list will not
859 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800860 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 * @param cursor the cursor used to provide the items.
862 * @param isCheckedColumn specifies the column name on the cursor to use to determine
863 * whether a checkbox is checked or not. It must return an integer value where 1
864 * means checked and 0 means unchecked.
865 * @param labelColumn The column name on the cursor containing the string to display in the
866 * label.
867 * @param listener notified when an item on the list is clicked. The dialog will not be
868 * dismissed when an item is clicked. It will only be dismissed if clicked on a
869 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
870 *
871 * @return This Builder object to allow for chaining of calls to set methods
872 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800873 public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 final OnMultiChoiceClickListener listener) {
875 P.mCursor = cursor;
876 P.mOnCheckboxClickListener = listener;
877 P.mIsCheckedColumn = isCheckedColumn;
878 P.mLabelColumn = labelColumn;
879 P.mIsMultiChoice = true;
880 return this;
881 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 /**
884 * Set a list of items to be displayed in the dialog as the content, you will be notified of
885 * the selected item via the supplied listener. This should be an array type i.e.
886 * R.array.foo The list will have a check mark displayed to the right of the text for the
887 * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a
888 * button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800889 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 * @param itemsId the resource id of an array i.e. R.array.foo
891 * @param checkedItem specifies which item is checked. If -1 no items are checked.
892 * @param listener notified when an item on the list is clicked. The dialog will not be
893 * dismissed when an item is clicked. It will only be dismissed if clicked on a
894 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
895 *
896 * @return This Builder object to allow for chaining of calls to set methods
897 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700898 public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 final OnClickListener listener) {
900 P.mItems = P.mContext.getResources().getTextArray(itemsId);
901 P.mOnClickListener = listener;
902 P.mCheckedItem = checkedItem;
903 P.mIsSingleChoice = true;
904 return this;
905 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 /**
908 * Set a list of items to be displayed in the dialog as the content, you will be notified of
909 * the selected item via the supplied listener. The list will have a check mark displayed to
910 * the right of the text for the checked item. Clicking on an item in the list will not
911 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800912 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 * @param cursor the cursor to retrieve the items from.
914 * @param checkedItem specifies which item is checked. If -1 no items are checked.
915 * @param labelColumn The column name on the cursor containing the string to display in the
916 * label.
917 * @param listener notified when an item on the list is clicked. The dialog will not be
918 * dismissed when an item is clicked. It will only be dismissed if clicked on a
919 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
920 *
921 * @return This Builder object to allow for chaining of calls to set methods
922 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800923 public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 final OnClickListener listener) {
925 P.mCursor = cursor;
926 P.mOnClickListener = listener;
927 P.mCheckedItem = checkedItem;
928 P.mLabelColumn = labelColumn;
929 P.mIsSingleChoice = true;
930 return this;
931 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 /**
934 * Set a list of items to be displayed in the dialog as the content, you will be notified of
935 * the selected item via the supplied listener. The list will have a check mark displayed to
936 * the right of the text for the checked item. Clicking on an item in the list will not
937 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800938 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 * @param items the items to be displayed.
940 * @param checkedItem specifies which item is checked. If -1 no items are checked.
941 * @param listener notified when an item on the list is clicked. The dialog will not be
942 * dismissed when an item is clicked. It will only be dismissed if clicked on a
943 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
944 *
945 * @return This Builder object to allow for chaining of calls to set methods
946 */
947 public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {
948 P.mItems = items;
949 P.mOnClickListener = listener;
950 P.mCheckedItem = checkedItem;
951 P.mIsSingleChoice = true;
952 return this;
Alan Viverette49f118e2015-02-23 17:15:27 -0800953 }
954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 /**
956 * Set a list of items to be displayed in the dialog as the content, you will be notified of
957 * the selected item via the supplied listener. The list will have a check mark displayed to
958 * the right of the text for the checked item. Clicking on an item in the list will not
959 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800960 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 * @param adapter The {@link ListAdapter} to supply the list of items
962 * @param checkedItem specifies which item is checked. If -1 no items are checked.
963 * @param listener notified when an item on the list is clicked. The dialog will not be
964 * dismissed when an item is clicked. It will only be dismissed if clicked on a
965 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
966 *
967 * @return This Builder object to allow for chaining of calls to set methods
968 */
969 public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) {
970 P.mAdapter = adapter;
971 P.mOnClickListener = listener;
972 P.mCheckedItem = checkedItem;
973 P.mIsSingleChoice = true;
974 return this;
975 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 /**
978 * Sets a listener to be invoked when an item in the list is selected.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 *
Alan Viverette49f118e2015-02-23 17:15:27 -0800980 * @param listener the listener to be invoked
981 * @return this Builder object to allow for chaining of calls to set methods
982 * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 */
984 public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {
985 P.mOnItemSelectedListener = listener;
986 return this;
987 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 /**
Alan Viveretteec186702013-12-05 11:10:31 -0800990 * Set a custom view resource to be the contents of the Dialog. The
991 * resource will be inflated, adding all top-level views to the screen.
992 *
993 * @param layoutResId Resource ID to be inflated.
Alan Viverette49f118e2015-02-23 17:15:27 -0800994 * @return this Builder object to allow for chaining of calls to set
Alan Viveretteec186702013-12-05 11:10:31 -0800995 * methods
996 */
997 public Builder setView(int layoutResId) {
998 P.mView = null;
999 P.mViewLayoutResId = layoutResId;
1000 P.mViewSpacingSpecified = false;
1001 return this;
1002 }
1003
1004 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001005 * Sets a custom view to be the contents of the alert dialog.
1006 * <p>
1007 * When using a pre-Holo theme, if the supplied view is an instance of
1008 * a {@link ListView} then the light background will be used.
1009 * <p>
1010 * <strong>Note:</strong> To ensure consistent styling, the custom view
1011 * should be inflated or constructed using the alert dialog's themed
1012 * context obtained via {@link #getContext()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 *
Alan Viverette49f118e2015-02-23 17:15:27 -08001014 * @param view the view to use as the contents of the alert dialog
1015 * @return this Builder object to allow for chaining of calls to set
1016 * methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 */
1018 public Builder setView(View view) {
1019 P.mView = view;
Alan Viveretteec186702013-12-05 11:10:31 -08001020 P.mViewLayoutResId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 P.mViewSpacingSpecified = false;
1022 return this;
1023 }
Alan Viverette49f118e2015-02-23 17:15:27 -08001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001026 * Sets a custom view to be the contents of the alert dialog and
1027 * specifies additional padding around that view.
1028 * <p>
1029 * When using a pre-Holo theme, if the supplied view is an instance of
1030 * a {@link ListView} then the light background will be used.
1031 * <p>
1032 * <strong>Note:</strong> To ensure consistent styling, the custom view
1033 * should be inflated or constructed using the alert dialog's themed
1034 * context obtained via {@link #getContext()}.
1035 *
1036 * @param view the view to use as the contents of the alert dialog
1037 * @param viewSpacingLeft spacing between the left edge of the view and
1038 * the dialog frame
1039 * @param viewSpacingTop spacing between the top edge of the view and
1040 * the dialog frame
1041 * @param viewSpacingRight spacing between the right edge of the view
1042 * and the dialog frame
1043 * @param viewSpacingBottom spacing between the bottom edge of the view
1044 * and the dialog frame
1045 * @return this Builder object to allow for chaining of calls to set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 * methods
Alan Viverette49f118e2015-02-23 17:15:27 -08001047 *
1048 * @hide Remove once the framework usages have been replaced.
1049 * @deprecated Set the padding on the view itself.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 */
Alan Viverette49f118e2015-02-23 17:15:27 -08001051 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
1053 int viewSpacingRight, int viewSpacingBottom) {
1054 P.mView = view;
Alan Viveretteec186702013-12-05 11:10:31 -08001055 P.mViewLayoutResId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 P.mViewSpacingSpecified = true;
1057 P.mViewSpacingLeft = viewSpacingLeft;
1058 P.mViewSpacingTop = viewSpacingTop;
1059 P.mViewSpacingRight = viewSpacingRight;
1060 P.mViewSpacingBottom = viewSpacingBottom;
1061 return this;
1062 }
Alan Viverette49f118e2015-02-23 17:15:27 -08001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001065 * Sets the alert dialog to use the inverse background, regardless of
1066 * what the contents is.
1067 *
1068 * @param useInverseBackground whether to use the inverse background
1069 * @return this Builder object to allow for chaining of calls to set methods
1070 * @deprecated This flag is only used for pre-Material themes. Instead,
1071 * specify the window background using on the alert dialog
1072 * theme.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 */
Alan Viverette49f118e2015-02-23 17:15:27 -08001074 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 public Builder setInverseBackgroundForced(boolean useInverseBackground) {
1076 P.mForceInverseBackground = useInverseBackground;
1077 return this;
1078 }
Romain Guy9c802c12009-03-25 15:07:31 -07001079
1080 /**
1081 * @hide
1082 */
1083 public Builder setRecycleOnMeasureEnabled(boolean enabled) {
1084 P.mRecycleOnMeasure = enabled;
1085 return this;
1086 }
1087
1088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001090 * Creates an {@link AlertDialog} with the arguments supplied to this
1091 * builder.
1092 * <p>
1093 * Calling this method does not display the dialog. If no additional
1094 * processing is needed, {@link #show()} may be called instead to both
1095 * create and display the dialog.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 */
1097 public AlertDialog create() {
Alan Viverette93d9e262015-08-11 15:02:42 -04001098 // Context has already been wrapped with the appropriate theme.
1099 final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 P.apply(dialog.mAlert);
Adam Powellfbca7692011-06-22 21:29:19 -07001101 dialog.setCancelable(P.mCancelable);
1102 if (P.mCancelable) {
1103 dialog.setCanceledOnTouchOutside(true);
1104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 dialog.setOnCancelListener(P.mOnCancelListener);
Adam Powell7f02dc52012-08-27 13:35:16 -07001106 dialog.setOnDismissListener(P.mOnDismissListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 if (P.mOnKeyListener != null) {
1108 dialog.setOnKeyListener(P.mOnKeyListener);
1109 }
1110 return dialog;
1111 }
1112
1113 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001114 * Creates an {@link AlertDialog} with the arguments supplied to this
1115 * builder and immediately displays the dialog.
1116 * <p>
1117 * Calling this method is functionally identical to:
1118 * <pre>
1119 * AlertDialog dialog = builder.create();
1120 * dialog.show();
1121 * </pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 */
1123 public AlertDialog show() {
Alan Viverette49f118e2015-02-23 17:15:27 -08001124 final AlertDialog dialog = create();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 dialog.show();
1126 return dialog;
1127 }
1128 }
Alan Viverette49f118e2015-02-23 17:15:27 -08001129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130}