blob: dbc8c5d1727b3d0812743aeac9ac0c397816b367 [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;
Mathew Inwood4fb17d12018-08-14 14:25:44 +010024import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.DialogInterface;
Adam Lesinski9553fb32017-05-23 18:53:44 -070027import android.content.res.ResourceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.database.Cursor;
29import android.graphics.drawable.Drawable;
30import android.os.Bundle;
31import android.os.Message;
Makoto Onuki778ce662018-04-20 14:04:50 -070032import android.text.Layout;
33import android.text.method.MovementMethod;
Adam Powell2fbf4de62010-09-30 15:46:46 -070034import android.util.TypedValue;
Adam Powellc49c1732010-09-28 16:03:15 -070035import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.view.KeyEvent;
37import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.widget.AdapterView;
39import android.widget.Button;
40import android.widget.ListAdapter;
41import android.widget.ListView;
42
Alan Viverette49f118e2015-02-23 17:15:27 -080043import com.android.internal.R;
Makoto Onuki778ce662018-04-20 14:04:50 -070044import com.android.internal.app.AlertController;
Alan Viverette49f118e2015-02-23 17:15:27 -080045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046/**
47 * A subclass of Dialog that can display one, two or three buttons. If you only want to
48 * display a String in this dialog box, use the setMessage() method. If you
Adam Powellc9006872010-03-18 13:44:45 -070049 * want to display a more complex view, look up the FrameLayout called "custom"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 * and add your view to it:
51 *
52 * <pre>
Alan Viverette51efddb2017-04-05 10:00:01 -040053 * FrameLayout fl = findViewById(android.R.id.custom);
Adam Powellc9006872010-03-18 13:44:45 -070054 * fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 * </pre>
Alan Viverette49f118e2015-02-23 17:15:27 -080056 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 * <p>The AlertDialog class takes care of automatically setting
Makoto Onuki778ce662018-04-20 14:04:50 -070058 * {@link android.view.WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 * WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} for you based on whether
60 * any views in the dialog return true from {@link View#onCheckIsTextEditor()
61 * View.onCheckIsTextEditor()}. Generally you want this set for a Dialog
62 * without text editors, so that it will be placed on top of the current
63 * input method UI. You can modify this behavior by forcing the flag to your
64 * desired mode after calling {@link #onCreate}.
Joe Fernandez558459f2011-10-13 16:47:36 -070065 *
66 * <div class="special reference">
67 * <h3>Developer Guides</h3>
68 * <p>For more information about creating dialogs, read the
69 * <a href="{@docRoot}guide/topics/ui/dialogs.html">Dialogs</a> developer guide.</p>
70 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 */
72public class AlertDialog extends Dialog implements DialogInterface {
Mathew Inwood4fb17d12018-08-14 14:25:44 +010073 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private AlertController mAlert;
75
Dianne Hackborne79b5542011-01-27 15:18:46 -080076 /**
77 * Special theme constant for {@link #AlertDialog(Context, int)}: use
78 * the traditional (pre-Holo) alert dialog theme.
Alan Viverette49f118e2015-02-23 17:15:27 -080079 *
80 * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
Dianne Hackborne79b5542011-01-27 15:18:46 -080081 */
Alan Viverette49f118e2015-02-23 17:15:27 -080082 @Deprecated
Dianne Hackborne79b5542011-01-27 15:18:46 -080083 public static final int THEME_TRADITIONAL = 1;
Alan Viverette49f118e2015-02-23 17:15:27 -080084
Dianne Hackborne79b5542011-01-27 15:18:46 -080085 /**
86 * Special theme constant for {@link #AlertDialog(Context, int)}: use
87 * the holographic alert theme with a dark background.
Alan Viverette49f118e2015-02-23 17:15:27 -080088 *
89 * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
Dianne Hackborne79b5542011-01-27 15:18:46 -080090 */
Alan Viverette49f118e2015-02-23 17:15:27 -080091 @Deprecated
Dianne Hackborne79b5542011-01-27 15:18:46 -080092 public static final int THEME_HOLO_DARK = 2;
Alan Viverette49f118e2015-02-23 17:15:27 -080093
Dianne Hackborne79b5542011-01-27 15:18:46 -080094 /**
95 * Special theme constant for {@link #AlertDialog(Context, int)}: use
96 * the holographic alert theme with a light background.
Alan Viverette49f118e2015-02-23 17:15:27 -080097 *
98 * @deprecated Use {@link android.R.style#Theme_Material_Light_Dialog_Alert}.
Dianne Hackborne79b5542011-01-27 15:18:46 -080099 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800100 @Deprecated
Dianne Hackborne79b5542011-01-27 15:18:46 -0800101 public static final int THEME_HOLO_LIGHT = 3;
Adam Powell6e90a362011-08-14 16:48:32 -0700102
103 /**
104 * Special theme constant for {@link #AlertDialog(Context, int)}: use
105 * the device's default alert theme with a dark background.
Alan Viverette49f118e2015-02-23 17:15:27 -0800106 *
107 * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Dialog_Alert}.
Adam Powell6e90a362011-08-14 16:48:32 -0700108 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800109 @Deprecated
Adam Powell6e90a362011-08-14 16:48:32 -0700110 public static final int THEME_DEVICE_DEFAULT_DARK = 4;
111
112 /**
113 * Special theme constant for {@link #AlertDialog(Context, int)}: use
Scott Kennedy7ed189e2013-01-11 22:31:43 -0800114 * the device's default alert theme with a light background.
Alan Viverette49f118e2015-02-23 17:15:27 -0800115 *
116 * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Light_Dialog_Alert}.
Adam Powell6e90a362011-08-14 16:48:32 -0700117 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800118 @Deprecated
Adam Powell6e90a362011-08-14 16:48:32 -0700119 public static final int THEME_DEVICE_DEFAULT_LIGHT = 5;
Craig Stout4c0cb8a2014-04-04 13:03:10 -0700120
121 /**
122 * No layout hint.
123 * @hide
124 */
125 public static final int LAYOUT_HINT_NONE = 0;
126
127 /**
128 * Hint layout to the side.
129 * @hide
130 */
131 public static final int LAYOUT_HINT_SIDE = 1;
Alan Viverette49f118e2015-02-23 17:15:27 -0800132
133 /**
134 * Creates an alert dialog that uses the default alert dialog theme.
135 * <p>
136 * The default alert dialog theme is defined by
137 * {@link android.R.attr#alertDialogTheme} within the parent
138 * {@code context}'s theme.
139 *
140 * @param context the parent context
Alan Viverette5696f042015-04-08 11:03:32 -0700141 * @see android.R.styleable#Theme_alertDialogTheme
Alan Viverette49f118e2015-02-23 17:15:27 -0800142 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 protected AlertDialog(Context context) {
Alan Viverette49f118e2015-02-23 17:15:27 -0800144 this(context, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
146
Dianne Hackborne79b5542011-01-27 15:18:46 -0800147 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800148 * Creates an alert dialog that uses the default alert dialog theme and a
149 * custom cancel listener.
150 * <p>
151 * This is functionally identical to:
152 * <pre>
153 * AlertDialog dialog = new AlertDialog(context);
154 * alertDialog.setCancelable(cancelable);
155 * alertDialog.setOnCancelListener(cancelListener);
156 * </pre>
157 * <p>
158 * The default alert dialog theme is defined by
159 * {@link android.R.attr#alertDialogTheme} within the parent
160 * {@code context}'s theme.
161 *
162 * @param context the parent context
Alan Viverette5696f042015-04-08 11:03:32 -0700163 * @see android.R.styleable#Theme_alertDialogTheme
Dianne Hackborne79b5542011-01-27 15:18:46 -0800164 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800165 protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
166 this(context, 0);
167
168 setCancelable(cancelable);
169 setOnCancelListener(cancelListener);
Dianne Hackborne79b5542011-01-27 15:18:46 -0800170 }
171
Alan Viverette49f118e2015-02-23 17:15:27 -0800172 /**
173 * Creates an alert dialog that uses an explicit theme resource.
174 * <p>
175 * The specified theme resource ({@code themeResId}) is applied on top of
176 * the parent {@code context}'s theme. It may be specified as a style
177 * resource containing a fully-populated theme, such as
178 * {@link android.R.style#Theme_Material_Dialog}, to replace all attributes
179 * in the parent {@code context}'s theme including primary and accent
180 * colors.
181 * <p>
182 * To preserve attributes such as primary and accent colors, the
183 * {@code themeResId} may instead be specified as an overlay theme such as
184 * {@link android.R.style#ThemeOverlay_Material_Dialog}. This will override
185 * only the window attributes necessary to style the alert window as a
186 * dialog.
187 * <p>
188 * Alternatively, the {@code themeResId} may be specified as {@code 0} to
189 * use the parent {@code context}'s resolved value for
190 * {@link android.R.attr#alertDialogTheme}.
191 *
192 * @param context the parent context
193 * @param themeResId the resource ID of the theme against which to inflate
194 * this dialog, or {@code 0} to use the parent
195 * {@code context}'s default alert dialog theme
Alan Viverette5696f042015-04-08 11:03:32 -0700196 * @see android.R.styleable#Theme_alertDialogTheme
Alan Viverette49f118e2015-02-23 17:15:27 -0800197 */
Alan Viverette682a4332015-04-10 11:05:50 -0700198 protected AlertDialog(Context context, @StyleRes int themeResId) {
Alan Viverette5696f042015-04-08 11:03:32 -0700199 this(context, themeResId, true);
200 }
201
Alan Viverette682a4332015-04-10 11:05:50 -0700202 AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
Alan Viverette5696f042015-04-08 11:03:32 -0700203 super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0,
204 createContextThemeWrapper);
Jeff Browna492c3a2012-08-23 19:48:44 -0700205
Dianne Hackborncfaf8872011-01-18 13:57:54 -0800206 mWindow.alwaysReadCloseOnTouchAttr();
Michael Kwancc6e6f02016-05-16 12:54:57 -0700207 mAlert = AlertController.create(getContext(), this, getWindow());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 }
209
Adam Lesinski9553fb32017-05-23 18:53:44 -0700210 static @StyleRes int resolveDialogTheme(Context context, @StyleRes int themeResId) {
Alan Viverette682a4332015-04-10 11:05:50 -0700211 if (themeResId == THEME_TRADITIONAL) {
Alan Viverette49f118e2015-02-23 17:15:27 -0800212 return R.style.Theme_Dialog_Alert;
213 } else if (themeResId == THEME_HOLO_DARK) {
214 return R.style.Theme_Holo_Dialog_Alert;
215 } else if (themeResId == THEME_HOLO_LIGHT) {
216 return R.style.Theme_Holo_Light_Dialog_Alert;
217 } else if (themeResId == THEME_DEVICE_DEFAULT_DARK) {
218 return R.style.Theme_DeviceDefault_Dialog_Alert;
219 } else if (themeResId == THEME_DEVICE_DEFAULT_LIGHT) {
220 return R.style.Theme_DeviceDefault_Light_Dialog_Alert;
Adam Lesinski9553fb32017-05-23 18:53:44 -0700221 } else if (ResourceId.isValid(themeResId)) {
Adam Lesinski36018212017-04-27 18:09:19 -0700222 // start of real resource IDs.
Alan Viverette49f118e2015-02-23 17:15:27 -0800223 return themeResId;
Dianne Hackborne79b5542011-01-27 15:18:46 -0800224 } else {
Alan Viverette49f118e2015-02-23 17:15:27 -0800225 final TypedValue outValue = new TypedValue();
226 context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true);
Dianne Hackborne79b5542011-01-27 15:18:46 -0800227 return outValue.resourceId;
228 }
Adam Powell2fbf4de62010-09-30 15:46:46 -0700229 }
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
Alan Viverettea1e63312013-12-09 18:20:19 -0800232 * Gets one of the buttons used in the dialog. Returns null if the specified
Alan Viverettef34ef742013-12-11 15:59:53 -0800233 * button does not exist or the dialog has not yet been fully created (for
234 * example, via {@link #show()} or {@link #create()}).
Alan Viverettea1e63312013-12-09 18:20:19 -0800235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * @param whichButton The identifier of the button that should be returned.
237 * For example, this can be
238 * {@link DialogInterface#BUTTON_POSITIVE}.
239 * @return The button from the dialog, or null if a button does not exist.
240 */
241 public Button getButton(int whichButton) {
242 return mAlert.getButton(whichButton);
243 }
Alan Viverettea1e63312013-12-09 18:20:19 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 /**
246 * Gets the list view used in the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * @return The {@link ListView} from the dialog.
249 */
250 public ListView getListView() {
251 return mAlert.getListView();
252 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 @Override
255 public void setTitle(CharSequence title) {
256 super.setTitle(title);
257 mAlert.setTitle(title);
258 }
259
260 /**
261 * @see Builder#setCustomTitle(View)
262 */
263 public void setCustomTitle(View customTitleView) {
264 mAlert.setCustomTitle(customTitleView);
265 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 public void setMessage(CharSequence message) {
268 mAlert.setMessage(message);
269 }
270
Makoto Onuki778ce662018-04-20 14:04:50 -0700271 /** @hide */
272 public void setMessageMovementMethod(MovementMethod movementMethod) {
273 mAlert.setMessageMovementMethod(movementMethod);
274 }
275
276 /** @hide */
277 public void setMessageHyphenationFrequency(
278 @Layout.HyphenationFrequency int hyphenationFrequency) {
279 mAlert.setMessageHyphenationFrequency(hyphenationFrequency);
280 }
281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * Set the view to display in that dialog.
284 */
285 public void setView(View view) {
286 mAlert.setView(view);
287 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800290 * 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 -0800291 * view.
292 *
293 * @param view The view to show in the content area of the dialog
294 * @param viewSpacingLeft Extra space to appear to the left of {@code view}
295 * @param viewSpacingTop Extra space to appear above {@code view}
296 * @param viewSpacingRight Extra space to appear to the right of {@code view}
297 * @param viewSpacingBottom Extra space to appear below {@code view}
298 */
299 public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,
300 int viewSpacingBottom) {
301 mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);
302 }
303
304 /**
Craig Stout4c0cb8a2014-04-04 13:03:10 -0700305 * Internal api to allow hinting for the best button panel layout.
306 * @hide
307 */
308 void setButtonPanelLayoutHint(int layoutHint) {
309 mAlert.setButtonPanelLayoutHint(layoutHint);
310 }
311
312 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * Set a message to be sent when a button is pressed.
Alan Viverette49f118e2015-02-23 17:15:27 -0800314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 * @param whichButton Which button to set the message for, can be one of
316 * {@link DialogInterface#BUTTON_POSITIVE},
317 * {@link DialogInterface#BUTTON_NEGATIVE}, or
318 * {@link DialogInterface#BUTTON_NEUTRAL}
319 * @param text The text to display in positive button.
320 * @param msg The {@link Message} to be sent when clicked.
321 */
322 public void setButton(int whichButton, CharSequence text, Message msg) {
323 mAlert.setButton(whichButton, text, null, msg);
324 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
327 * Set a listener to be invoked when the positive button of the dialog is pressed.
Alan Viverette49f118e2015-02-23 17:15:27 -0800328 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 * @param whichButton Which button to set the listener on, can be one of
330 * {@link DialogInterface#BUTTON_POSITIVE},
331 * {@link DialogInterface#BUTTON_NEGATIVE}, or
332 * {@link DialogInterface#BUTTON_NEUTRAL}
333 * @param text The text to display in positive button.
334 * @param listener The {@link DialogInterface.OnClickListener} to use.
335 */
336 public void setButton(int whichButton, CharSequence text, OnClickListener listener) {
337 mAlert.setButton(whichButton, text, listener, null);
338 }
339
340 /**
341 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
342 * {@link DialogInterface#BUTTON_POSITIVE}.
343 */
344 @Deprecated
345 public void setButton(CharSequence text, Message msg) {
346 setButton(BUTTON_POSITIVE, text, msg);
347 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
351 * {@link DialogInterface#BUTTON_NEGATIVE}.
352 */
353 @Deprecated
354 public void setButton2(CharSequence text, Message msg) {
355 setButton(BUTTON_NEGATIVE, text, msg);
356 }
357
358 /**
359 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with
360 * {@link DialogInterface#BUTTON_NEUTRAL}.
361 */
362 @Deprecated
363 public void setButton3(CharSequence text, Message msg) {
364 setButton(BUTTON_NEUTRAL, text, msg);
365 }
366
367 /**
368 * Set a listener to be invoked when button 1 of the dialog is pressed.
Alan Viverette49f118e2015-02-23 17:15:27 -0800369 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 * @param text The text to display in button 1.
371 * @param listener The {@link DialogInterface.OnClickListener} to use.
372 * @deprecated Use
373 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
374 * with {@link DialogInterface#BUTTON_POSITIVE}
375 */
376 @Deprecated
377 public void setButton(CharSequence text, final OnClickListener listener) {
378 setButton(BUTTON_POSITIVE, text, listener);
379 }
380
381 /**
382 * Set a listener to be invoked when button 2 of the dialog is pressed.
383 * @param text The text to display in button 2.
384 * @param listener The {@link DialogInterface.OnClickListener} to use.
385 * @deprecated Use
386 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
387 * with {@link DialogInterface#BUTTON_NEGATIVE}
388 */
389 @Deprecated
390 public void setButton2(CharSequence text, final OnClickListener listener) {
391 setButton(BUTTON_NEGATIVE, text, listener);
392 }
393
394 /**
395 * Set a listener to be invoked when button 3 of the dialog is pressed.
396 * @param text The text to display in button 3.
397 * @param listener The {@link DialogInterface.OnClickListener} to use.
398 * @deprecated Use
399 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)}
400 * with {@link DialogInterface#BUTTON_POSITIVE}
401 */
402 @Deprecated
403 public void setButton3(CharSequence text, final OnClickListener listener) {
404 setButton(BUTTON_NEUTRAL, text, listener);
405 }
406
407 /**
408 * Set resId to 0 if you don't want an icon.
409 * @param resId the resourceId of the drawable to use as the icon or 0
410 * if you don't want an icon.
411 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700412 public void setIcon(@DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 mAlert.setIcon(resId);
414 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 public void setIcon(Drawable icon) {
417 mAlert.setIcon(icon);
418 }
419
Adam Powell947f7822011-01-07 22:30:48 -0800420 /**
421 * Set an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon
422 *
423 * @param attrId ID of a theme attribute that points to a drawable resource.
424 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700425 public void setIconAttribute(@AttrRes int attrId) {
Adam Powell947f7822011-01-07 22:30:48 -0800426 TypedValue out = new TypedValue();
427 mContext.getTheme().resolveAttribute(attrId, out, true);
428 mAlert.setIcon(out.resourceId);
429 }
430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 public void setInverseBackgroundForced(boolean forceInverseBackground) {
432 mAlert.setInverseBackgroundForced(forceInverseBackground);
433 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 @Override
436 protected void onCreate(Bundle savedInstanceState) {
437 super.onCreate(savedInstanceState);
438 mAlert.installContent();
439 }
440
441 @Override
442 public boolean onKeyDown(int keyCode, KeyEvent event) {
443 if (mAlert.onKeyDown(keyCode, event)) return true;
444 return super.onKeyDown(keyCode, event);
445 }
446
447 @Override
448 public boolean onKeyUp(int keyCode, KeyEvent event) {
449 if (mAlert.onKeyUp(keyCode, event)) return true;
450 return super.onKeyUp(keyCode, event);
451 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 public static class Builder {
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100454 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 private final AlertController.AlertParams P;
Alan Viverette49f118e2015-02-23 17:15:27 -0800456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800458 * Creates a builder for an alert dialog that uses the default alert
459 * dialog theme.
460 * <p>
461 * The default alert dialog theme is defined by
462 * {@link android.R.attr#alertDialogTheme} within the parent
463 * {@code context}'s theme.
464 *
465 * @param context the parent context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 */
467 public Builder(Context context) {
Adam Lesinski9553fb32017-05-23 18:53:44 -0700468 this(context, resolveDialogTheme(context, ResourceId.ID_NULL));
Martin Nordholtse429c5e2010-06-15 16:17:24 +0200469 }
470
471 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800472 * Creates a builder for an alert dialog that uses an explicit theme
473 * resource.
474 * <p>
475 * The specified theme resource ({@code themeResId}) is applied on top
476 * of the parent {@code context}'s theme. It may be specified as a
477 * style resource containing a fully-populated theme, such as
478 * {@link android.R.style#Theme_Material_Dialog}, to replace all
479 * attributes in the parent {@code context}'s theme including primary
480 * and accent colors.
481 * <p>
482 * To preserve attributes such as primary and accent colors, the
483 * {@code themeResId} may instead be specified as an overlay theme such
484 * as {@link android.R.style#ThemeOverlay_Material_Dialog}. This will
485 * override only the window attributes necessary to style the alert
486 * window as a dialog.
487 * <p>
488 * Alternatively, the {@code themeResId} may be specified as {@code 0}
489 * to use the parent {@code context}'s resolved value for
490 * {@link android.R.attr#alertDialogTheme}.
491 *
492 * @param context the parent context
493 * @param themeResId the resource ID of the theme against which to inflate
494 * this dialog, or {@code 0} to use the parent
495 * {@code context}'s default alert dialog theme
Martin Nordholtse429c5e2010-06-15 16:17:24 +0200496 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800497 public Builder(Context context, int themeResId) {
Dianne Hackborne79b5542011-01-27 15:18:46 -0800498 P = new AlertController.AlertParams(new ContextThemeWrapper(
Alan Viverette49f118e2015-02-23 17:15:27 -0800499 context, resolveDialogTheme(context, themeResId)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 /**
Adam Powellc49c1732010-09-28 16:03:15 -0700503 * Returns a {@link Context} with the appropriate theme for dialogs created by this Builder.
504 * Applications should use this Context for obtaining LayoutInflaters for inflating views
505 * that will be used in the resulting dialogs, as it will cause views to be inflated with
506 * the correct theme.
507 *
508 * @return A Context for built Dialogs.
509 */
510 public Context getContext() {
Adam Powellccb013f2010-10-12 18:42:56 -0700511 return P.mContext;
Adam Powellc49c1732010-09-28 16:03:15 -0700512 }
513
514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 * Set the title using the given resource id.
516 *
517 * @return This Builder object to allow for chaining of calls to set methods
518 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700519 public Builder setTitle(@StringRes int titleId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 P.mTitle = P.mContext.getText(titleId);
521 return this;
522 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 /**
525 * Set the title displayed in the {@link Dialog}.
526 *
527 * @return This Builder object to allow for chaining of calls to set methods
528 */
529 public Builder setTitle(CharSequence title) {
530 P.mTitle = title;
531 return this;
532 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 /**
Alan Viverette49f118e2015-02-23 17:15:27 -0800535 * Set the title using the custom view {@code customTitleView}.
536 * <p>
537 * The methods {@link #setTitle(int)} and {@link #setIcon(int)} should
538 * be sufficient for most titles, but this is provided if the title
539 * needs more customization. Using this will replace the title and icon
540 * set via the other methods.
541 * <p>
542 * <strong>Note:</strong> To ensure consistent styling, the custom view
543 * should be inflated or constructed using the alert dialog's themed
544 * context obtained via {@link #getContext()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 *
Alan Viverette49f118e2015-02-23 17:15:27 -0800546 * @param customTitleView the custom view to use as the title
547 * @return this Builder object to allow for chaining of calls to set
548 * methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 */
550 public Builder setCustomTitle(View customTitleView) {
551 P.mCustomTitleView = customTitleView;
552 return this;
553 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 /**
556 * Set the message to display using the given resource id.
557 *
558 * @return This Builder object to allow for chaining of calls to set methods
559 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700560 public Builder setMessage(@StringRes int messageId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 P.mMessage = P.mContext.getText(messageId);
562 return this;
563 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 /**
566 * Set the message to display.
567 *
568 * @return This Builder object to allow for chaining of calls to set methods
569 */
570 public Builder setMessage(CharSequence message) {
571 P.mMessage = message;
572 return this;
573 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 /**
576 * Set the resource id of the {@link Drawable} to be used in the title.
Alan Viverette5c2d8f72015-01-05 12:56:45 -0800577 * <p>
578 * Takes precedence over values set using {@link #setIcon(Drawable)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 *
580 * @return This Builder object to allow for chaining of calls to set methods
581 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700582 public Builder setIcon(@DrawableRes int iconId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 P.mIconId = iconId;
584 return this;
585 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 /**
588 * Set the {@link Drawable} to be used in the title.
Alan Viverette49f118e2015-02-23 17:15:27 -0800589 * <p>
590 * <strong>Note:</strong> To ensure consistent styling, the drawable
591 * should be inflated or constructed using the alert dialog's themed
592 * context obtained via {@link #getContext()}.
593 *
594 * @return this Builder object to allow for chaining of calls to set
595 * methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 */
597 public Builder setIcon(Drawable icon) {
598 P.mIcon = icon;
599 return this;
600 }
Adam Powell947f7822011-01-07 22:30:48 -0800601
602 /**
Alan Viverette5c2d8f72015-01-05 12:56:45 -0800603 * Set an icon as supplied by a theme attribute. e.g.
604 * {@link android.R.attr#alertDialogIcon}.
605 * <p>
606 * Takes precedence over values set using {@link #setIcon(int)} or
607 * {@link #setIcon(Drawable)}.
Adam Powell947f7822011-01-07 22:30:48 -0800608 *
609 * @param attrId ID of a theme attribute that points to a drawable resource.
610 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700611 public Builder setIconAttribute(@AttrRes int attrId) {
Adam Powell947f7822011-01-07 22:30:48 -0800612 TypedValue out = new TypedValue();
613 P.mContext.getTheme().resolveAttribute(attrId, out, true);
614 P.mIconId = out.resourceId;
615 return this;
616 }
617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 /**
619 * Set a listener to be invoked when the positive button of the dialog is pressed.
620 * @param textId The resource id of the text to display in the positive button
621 * @param listener The {@link DialogInterface.OnClickListener} to use.
622 *
623 * @return This Builder object to allow for chaining of calls to set methods
624 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700625 public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 P.mPositiveButtonText = P.mContext.getText(textId);
627 P.mPositiveButtonListener = listener;
628 return this;
629 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 /**
632 * Set a listener to be invoked when the positive button of the dialog is pressed.
633 * @param text The text to display in the positive button
634 * @param listener The {@link DialogInterface.OnClickListener} to use.
635 *
636 * @return This Builder object to allow for chaining of calls to set methods
637 */
638 public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
639 P.mPositiveButtonText = text;
640 P.mPositiveButtonListener = listener;
641 return this;
642 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 /**
645 * Set a listener to be invoked when the negative button of the dialog is pressed.
646 * @param textId The resource id of the text to display in the negative button
647 * @param listener The {@link DialogInterface.OnClickListener} to use.
648 *
649 * @return This Builder object to allow for chaining of calls to set methods
650 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700651 public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 P.mNegativeButtonText = P.mContext.getText(textId);
653 P.mNegativeButtonListener = listener;
654 return this;
655 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 /**
658 * Set a listener to be invoked when the negative button of the dialog is pressed.
659 * @param text The text to display in the negative button
660 * @param listener The {@link DialogInterface.OnClickListener} to use.
661 *
662 * @return This Builder object to allow for chaining of calls to set methods
663 */
664 public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
665 P.mNegativeButtonText = text;
666 P.mNegativeButtonListener = listener;
667 return this;
668 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 /**
671 * Set a listener to be invoked when the neutral button of the dialog is pressed.
672 * @param textId The resource id of the text to display in the neutral button
673 * @param listener The {@link DialogInterface.OnClickListener} to use.
674 *
675 * @return This Builder object to allow for chaining of calls to set methods
676 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700677 public Builder setNeutralButton(@StringRes int textId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 P.mNeutralButtonText = P.mContext.getText(textId);
679 P.mNeutralButtonListener = listener;
680 return this;
681 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 /**
684 * Set a listener to be invoked when the neutral button of the dialog is pressed.
685 * @param text The text to display in the neutral button
686 * @param listener The {@link DialogInterface.OnClickListener} to use.
687 *
688 * @return This Builder object to allow for chaining of calls to set methods
689 */
690 public Builder setNeutralButton(CharSequence text, final OnClickListener listener) {
691 P.mNeutralButtonText = text;
692 P.mNeutralButtonListener = listener;
693 return this;
694 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 /**
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -0800697 * Sets whether the dialog is cancelable or not. Default is true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 *
699 * @return This Builder object to allow for chaining of calls to set methods
700 */
701 public Builder setCancelable(boolean cancelable) {
702 P.mCancelable = cancelable;
703 return this;
704 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 /**
707 * Sets the callback that will be called if the dialog is canceled.
Adam Powell7f02dc52012-08-27 13:35:16 -0700708 *
709 * <p>Even in a cancelable dialog, the dialog may be dismissed for reasons other than
710 * being canceled or one of the supplied choices being selected.
711 * If you are interested in listening for all cases where the dialog is dismissed
712 * and not just when it is canceled, see
713 * {@link #setOnDismissListener(android.content.DialogInterface.OnDismissListener) setOnDismissListener}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 * @see #setCancelable(boolean)
Adam Powell7f02dc52012-08-27 13:35:16 -0700715 * @see #setOnDismissListener(android.content.DialogInterface.OnDismissListener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 *
717 * @return This Builder object to allow for chaining of calls to set methods
718 */
719 public Builder setOnCancelListener(OnCancelListener onCancelListener) {
720 P.mOnCancelListener = onCancelListener;
721 return this;
722 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 /**
Adam Powell7f02dc52012-08-27 13:35:16 -0700725 * Sets the callback that will be called when the dialog is dismissed for any reason.
726 *
727 * @return This Builder object to allow for chaining of calls to set methods
728 */
729 public Builder setOnDismissListener(OnDismissListener onDismissListener) {
730 P.mOnDismissListener = onDismissListener;
731 return this;
732 }
733
734 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 * Sets the callback that will be called if a key is dispatched to the dialog.
736 *
737 * @return This Builder object to allow for chaining of calls to set methods
738 */
739 public Builder setOnKeyListener(OnKeyListener onKeyListener) {
740 P.mOnKeyListener = onKeyListener;
741 return this;
742 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 /**
745 * Set a list of items to be displayed in the dialog as the content, you will be notified of the
746 * selected item via the supplied listener. This should be an array type i.e. R.array.foo
747 *
748 * @return This Builder object to allow for chaining of calls to set methods
749 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700750 public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 P.mItems = P.mContext.getResources().getTextArray(itemsId);
752 P.mOnClickListener = listener;
753 return this;
754 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 /**
757 * Set a list of items to be displayed in the dialog as the content, you will be notified of the
758 * selected item via the supplied listener.
759 *
760 * @return This Builder object to allow for chaining of calls to set methods
761 */
762 public Builder setItems(CharSequence[] items, final OnClickListener listener) {
763 P.mItems = items;
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 ListAdapter}, 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 adapter The {@link ListAdapter} to supply the list of items
774 * @param listener The listener that will be called when an item is clicked.
775 *
776 * @return This Builder object to allow for chaining of calls to set methods
777 */
778 public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) {
779 P.mAdapter = adapter;
780 P.mOnClickListener = listener;
781 return this;
782 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 /**
785 * Set a list of items, which are supplied by the given {@link Cursor}, to be
786 * displayed in the dialog as the content, you will be notified of the
787 * selected item via the supplied listener.
Alan Viverette49f118e2015-02-23 17:15:27 -0800788 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 * @param cursor The {@link Cursor} to supply the list of items
790 * @param listener The listener that will be called when an item is clicked.
791 * @param labelColumn The column name on the cursor containing the string to display
792 * in the label.
793 *
794 * @return This Builder object to allow for chaining of calls to set methods
795 */
796 public Builder setCursor(final Cursor cursor, final OnClickListener listener,
797 String labelColumn) {
798 P.mCursor = cursor;
799 P.mLabelColumn = labelColumn;
800 P.mOnClickListener = listener;
801 return this;
802 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 /**
805 * Set a list of items to be displayed in the dialog as the content,
806 * you will be notified of the selected item via the supplied listener.
807 * This should be an array type, e.g. R.array.foo. The list will have
808 * a check mark displayed to the right of the text for each checked
809 * item. Clicking on an item in the list will not dismiss the dialog.
810 * Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800811 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 * @param itemsId the resource id of an array i.e. R.array.foo
813 * @param checkedItems specifies which items are checked. It should be null in which case no
814 * items are checked. If non null it must be exactly the same length as the array of
815 * items.
816 * @param listener notified when an item on the list is clicked. The dialog will not be
817 * dismissed when an item is clicked. It will only be dismissed if clicked on a
818 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
819 *
820 * @return This Builder object to allow for chaining of calls to set methods
821 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700822 public Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 final OnMultiChoiceClickListener listener) {
824 P.mItems = P.mContext.getResources().getTextArray(itemsId);
825 P.mOnCheckboxClickListener = listener;
826 P.mCheckedItems = checkedItems;
827 P.mIsMultiChoice = true;
828 return this;
829 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 /**
832 * Set a list of items to be displayed in the dialog as the content,
833 * you will be notified of the selected item via the supplied listener.
834 * The list will have a check mark displayed to the right of the text
835 * for each checked item. Clicking on an item in the list will not
836 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800837 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 * @param items the text of the items to be displayed in the list.
839 * @param checkedItems specifies which items are checked. It should be null in which case no
840 * items are checked. If non null it must be exactly the same length as the array of
841 * items.
842 * @param listener notified when an item on the list is clicked. The dialog will not be
843 * dismissed when an item is clicked. It will only be dismissed if clicked on a
844 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
845 *
846 * @return This Builder object to allow for chaining of calls to set methods
847 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800848 public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 final OnMultiChoiceClickListener listener) {
850 P.mItems = items;
851 P.mOnCheckboxClickListener = listener;
852 P.mCheckedItems = checkedItems;
853 P.mIsMultiChoice = true;
854 return this;
855 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 /**
858 * Set a list of items to be displayed in the dialog as the content,
859 * you will be notified of the selected item via the supplied listener.
860 * The list will have a check mark displayed to the right of the text
861 * for each checked item. Clicking on an item in the list will not
862 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 * @param cursor the cursor used to provide the items.
865 * @param isCheckedColumn specifies the column name on the cursor to use to determine
866 * whether a checkbox is checked or not. It must return an integer value where 1
867 * means checked and 0 means unchecked.
868 * @param labelColumn The column name on the cursor containing the string to display in the
869 * label.
870 * @param listener notified when an item on the list is clicked. The dialog will not be
871 * dismissed when an item is clicked. It will only be dismissed if clicked on a
872 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
873 *
874 * @return This Builder object to allow for chaining of calls to set methods
875 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800876 public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 final OnMultiChoiceClickListener listener) {
878 P.mCursor = cursor;
879 P.mOnCheckboxClickListener = listener;
880 P.mIsCheckedColumn = isCheckedColumn;
881 P.mLabelColumn = labelColumn;
882 P.mIsMultiChoice = true;
883 return this;
884 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 /**
887 * Set a list of items to be displayed in the dialog as the content, you will be notified of
888 * the selected item via the supplied listener. This should be an array type i.e.
889 * R.array.foo The list will have a check mark displayed to the right of the text for the
890 * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a
891 * button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800892 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 * @param itemsId the resource id of an array i.e. R.array.foo
894 * @param checkedItem specifies which item is checked. If -1 no items are checked.
895 * @param listener notified when an item on the list is clicked. The dialog will not be
896 * dismissed when an item is clicked. It will only be dismissed if clicked on a
897 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
898 *
899 * @return This Builder object to allow for chaining of calls to set methods
900 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700901 public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 final OnClickListener listener) {
903 P.mItems = P.mContext.getResources().getTextArray(itemsId);
904 P.mOnClickListener = listener;
905 P.mCheckedItem = checkedItem;
906 P.mIsSingleChoice = true;
907 return this;
908 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 /**
911 * Set a list of items to be displayed in the dialog as the content, you will be notified of
912 * the selected item via the supplied listener. The list will have a check mark displayed to
913 * the right of the text for the checked item. Clicking on an item in the list will not
914 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800915 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 * @param cursor the cursor to retrieve the items from.
917 * @param checkedItem specifies which item is checked. If -1 no items are checked.
918 * @param labelColumn The column name on the cursor containing the string to display in the
919 * label.
920 * @param listener notified when an item on the list is clicked. The dialog will not be
921 * dismissed when an item is clicked. It will only be dismissed if clicked on a
922 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
923 *
924 * @return This Builder object to allow for chaining of calls to set methods
925 */
Alan Viverette49f118e2015-02-23 17:15:27 -0800926 public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 final OnClickListener listener) {
928 P.mCursor = cursor;
929 P.mOnClickListener = listener;
930 P.mCheckedItem = checkedItem;
931 P.mLabelColumn = labelColumn;
932 P.mIsSingleChoice = true;
933 return this;
934 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 /**
937 * Set a list of items to be displayed in the dialog as the content, you will be notified of
938 * the selected item via the supplied listener. The list will have a check mark displayed to
939 * the right of the text for the checked item. Clicking on an item in the list will not
940 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800941 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 * @param items the items to be displayed.
943 * @param checkedItem specifies which item is checked. If -1 no items are checked.
944 * @param listener notified when an item on the list is clicked. The dialog will not be
945 * dismissed when an item is clicked. It will only be dismissed if clicked on a
946 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
947 *
948 * @return This Builder object to allow for chaining of calls to set methods
949 */
950 public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {
951 P.mItems = items;
952 P.mOnClickListener = listener;
953 P.mCheckedItem = checkedItem;
954 P.mIsSingleChoice = true;
955 return this;
Alan Viverette49f118e2015-02-23 17:15:27 -0800956 }
957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 /**
959 * Set a list of items to be displayed in the dialog as the content, you will be notified of
960 * the selected item via the supplied listener. The list will have a check mark displayed to
961 * the right of the text for the checked item. Clicking on an item in the list will not
962 * dismiss the dialog. Clicking on a button will dismiss the dialog.
Alan Viverette49f118e2015-02-23 17:15:27 -0800963 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 * @param adapter The {@link ListAdapter} to supply the list of items
965 * @param checkedItem specifies which item is checked. If -1 no items are checked.
966 * @param listener notified when an item on the list is clicked. The dialog will not be
967 * dismissed when an item is clicked. It will only be dismissed if clicked on a
968 * button, if no buttons are supplied it's up to the user to dismiss the dialog.
969 *
970 * @return This Builder object to allow for chaining of calls to set methods
971 */
972 public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) {
973 P.mAdapter = adapter;
974 P.mOnClickListener = listener;
975 P.mCheckedItem = checkedItem;
976 P.mIsSingleChoice = true;
977 return this;
978 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 /**
981 * Sets a listener to be invoked when an item in the list is selected.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 *
Alan Viverette49f118e2015-02-23 17:15:27 -0800983 * @param listener the listener to be invoked
984 * @return this Builder object to allow for chaining of calls to set methods
985 * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 */
987 public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {
988 P.mOnItemSelectedListener = listener;
989 return this;
990 }
Alan Viverette49f118e2015-02-23 17:15:27 -0800991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 /**
Alan Viveretteec186702013-12-05 11:10:31 -0800993 * Set a custom view resource to be the contents of the Dialog. The
994 * resource will be inflated, adding all top-level views to the screen.
995 *
996 * @param layoutResId Resource ID to be inflated.
Alan Viverette49f118e2015-02-23 17:15:27 -0800997 * @return this Builder object to allow for chaining of calls to set
Alan Viveretteec186702013-12-05 11:10:31 -0800998 * methods
999 */
1000 public Builder setView(int layoutResId) {
1001 P.mView = null;
1002 P.mViewLayoutResId = layoutResId;
1003 P.mViewSpacingSpecified = false;
1004 return this;
1005 }
1006
1007 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001008 * Sets a custom view to be the contents of the alert dialog.
1009 * <p>
1010 * When using a pre-Holo theme, if the supplied view is an instance of
1011 * a {@link ListView} then the light background will be used.
1012 * <p>
1013 * <strong>Note:</strong> To ensure consistent styling, the custom view
1014 * should be inflated or constructed using the alert dialog's themed
1015 * context obtained via {@link #getContext()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 *
Alan Viverette49f118e2015-02-23 17:15:27 -08001017 * @param view the view to use as the contents of the alert dialog
1018 * @return this Builder object to allow for chaining of calls to set
1019 * methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 */
1021 public Builder setView(View view) {
1022 P.mView = view;
Alan Viveretteec186702013-12-05 11:10:31 -08001023 P.mViewLayoutResId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 P.mViewSpacingSpecified = false;
1025 return this;
1026 }
Alan Viverette49f118e2015-02-23 17:15:27 -08001027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001029 * Sets a custom view to be the contents of the alert dialog and
1030 * specifies additional padding around that view.
1031 * <p>
1032 * When using a pre-Holo theme, if the supplied view is an instance of
1033 * a {@link ListView} then the light background will be used.
1034 * <p>
1035 * <strong>Note:</strong> To ensure consistent styling, the custom view
1036 * should be inflated or constructed using the alert dialog's themed
1037 * context obtained via {@link #getContext()}.
1038 *
1039 * @param view the view to use as the contents of the alert dialog
1040 * @param viewSpacingLeft spacing between the left edge of the view and
1041 * the dialog frame
1042 * @param viewSpacingTop spacing between the top edge of the view and
1043 * the dialog frame
1044 * @param viewSpacingRight spacing between the right edge of the view
1045 * and the dialog frame
1046 * @param viewSpacingBottom spacing between the bottom edge of the view
1047 * and the dialog frame
1048 * @return this Builder object to allow for chaining of calls to set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 * methods
Alan Viverette49f118e2015-02-23 17:15:27 -08001050 *
1051 * @hide Remove once the framework usages have been replaced.
1052 * @deprecated Set the padding on the view itself.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 */
Alan Viverette49f118e2015-02-23 17:15:27 -08001054 @Deprecated
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001055 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
1057 int viewSpacingRight, int viewSpacingBottom) {
1058 P.mView = view;
Alan Viveretteec186702013-12-05 11:10:31 -08001059 P.mViewLayoutResId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 P.mViewSpacingSpecified = true;
1061 P.mViewSpacingLeft = viewSpacingLeft;
1062 P.mViewSpacingTop = viewSpacingTop;
1063 P.mViewSpacingRight = viewSpacingRight;
1064 P.mViewSpacingBottom = viewSpacingBottom;
1065 return this;
1066 }
Alan Viverette49f118e2015-02-23 17:15:27 -08001067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001069 * Sets the alert dialog to use the inverse background, regardless of
1070 * what the contents is.
1071 *
1072 * @param useInverseBackground whether to use the inverse background
1073 * @return this Builder object to allow for chaining of calls to set methods
1074 * @deprecated This flag is only used for pre-Material themes. Instead,
1075 * specify the window background using on the alert dialog
1076 * theme.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 */
Alan Viverette49f118e2015-02-23 17:15:27 -08001078 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 public Builder setInverseBackgroundForced(boolean useInverseBackground) {
1080 P.mForceInverseBackground = useInverseBackground;
1081 return this;
1082 }
Romain Guy9c802c12009-03-25 15:07:31 -07001083
1084 /**
1085 * @hide
1086 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001087 @UnsupportedAppUsage
Romain Guy9c802c12009-03-25 15:07:31 -07001088 public Builder setRecycleOnMeasureEnabled(boolean enabled) {
1089 P.mRecycleOnMeasure = enabled;
1090 return this;
1091 }
1092
1093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001095 * Creates an {@link AlertDialog} with the arguments supplied to this
1096 * builder.
1097 * <p>
1098 * Calling this method does not display the dialog. If no additional
1099 * processing is needed, {@link #show()} may be called instead to both
1100 * create and display the dialog.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 */
1102 public AlertDialog create() {
Alan Viverette93d9e262015-08-11 15:02:42 -04001103 // Context has already been wrapped with the appropriate theme.
1104 final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 P.apply(dialog.mAlert);
Adam Powellfbca7692011-06-22 21:29:19 -07001106 dialog.setCancelable(P.mCancelable);
1107 if (P.mCancelable) {
1108 dialog.setCanceledOnTouchOutside(true);
1109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 dialog.setOnCancelListener(P.mOnCancelListener);
Adam Powell7f02dc52012-08-27 13:35:16 -07001111 dialog.setOnDismissListener(P.mOnDismissListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 if (P.mOnKeyListener != null) {
1113 dialog.setOnKeyListener(P.mOnKeyListener);
1114 }
1115 return dialog;
1116 }
1117
1118 /**
Alan Viverette49f118e2015-02-23 17:15:27 -08001119 * Creates an {@link AlertDialog} with the arguments supplied to this
1120 * builder and immediately displays the dialog.
1121 * <p>
1122 * Calling this method is functionally identical to:
1123 * <pre>
1124 * AlertDialog dialog = builder.create();
1125 * dialog.show();
1126 * </pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 */
1128 public AlertDialog show() {
Alan Viverette49f118e2015-02-23 17:15:27 -08001129 final AlertDialog dialog = create();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 dialog.show();
1131 return dialog;
1132 }
1133 }
Alan Viverette49f118e2015-02-23 17:15:27 -08001134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135}