The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 19 | import android.annotation.ArrayRes; |
| 20 | import android.annotation.AttrRes; |
| 21 | import android.annotation.DrawableRes; |
| 22 | import android.annotation.StringRes; |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 23 | import android.annotation.StyleRes; |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 24 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
Adam Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 27 | import android.content.res.ResourceId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.database.Cursor; |
| 29 | import android.graphics.drawable.Drawable; |
| 30 | import android.os.Bundle; |
| 31 | import android.os.Message; |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 32 | import android.text.Layout; |
| 33 | import android.text.method.MovementMethod; |
Adam Powell | 2fbf4de6 | 2010-09-30 15:46:46 -0700 | [diff] [blame] | 34 | import android.util.TypedValue; |
Adam Powell | c49c173 | 2010-09-28 16:03:15 -0700 | [diff] [blame] | 35 | import android.view.ContextThemeWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.view.KeyEvent; |
| 37 | import android.view.View; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.widget.AdapterView; |
| 39 | import android.widget.Button; |
| 40 | import android.widget.ListAdapter; |
| 41 | import android.widget.ListView; |
| 42 | |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 43 | import com.android.internal.R; |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 44 | import com.android.internal.app.AlertController; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | /** |
| 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 Powell | c900687 | 2010-03-18 13:44:45 -0700 | [diff] [blame] | 49 | * want to display a more complex view, look up the FrameLayout called "custom" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * and add your view to it: |
| 51 | * |
| 52 | * <pre> |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 53 | * FrameLayout fl = findViewById(android.R.id.custom); |
Adam Powell | c900687 | 2010-03-18 13:44:45 -0700 | [diff] [blame] | 54 | * fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | * </pre> |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 56 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | * <p>The AlertDialog class takes care of automatically setting |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 58 | * {@link android.view.WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | * 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 Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 65 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | */ |
| 72 | public class AlertDialog extends Dialog implements DialogInterface { |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 73 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | private AlertController mAlert; |
| 75 | |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
| 78 | * the traditional (pre-Holo) alert dialog theme. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 79 | * |
| 80 | * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}. |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 81 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 82 | @Deprecated |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 83 | public static final int THEME_TRADITIONAL = 1; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 84 | |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
| 87 | * the holographic alert theme with a dark background. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 88 | * |
| 89 | * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}. |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 90 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 91 | @Deprecated |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 92 | public static final int THEME_HOLO_DARK = 2; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 93 | |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
| 96 | * the holographic alert theme with a light background. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 97 | * |
| 98 | * @deprecated Use {@link android.R.style#Theme_Material_Light_Dialog_Alert}. |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 99 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 100 | @Deprecated |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 101 | public static final int THEME_HOLO_LIGHT = 3; |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
| 105 | * the device's default alert theme with a dark background. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 106 | * |
| 107 | * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Dialog_Alert}. |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 108 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 109 | @Deprecated |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 110 | public static final int THEME_DEVICE_DEFAULT_DARK = 4; |
| 111 | |
| 112 | /** |
| 113 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
Scott Kennedy | 7ed189e | 2013-01-11 22:31:43 -0800 | [diff] [blame] | 114 | * the device's default alert theme with a light background. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 115 | * |
| 116 | * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Light_Dialog_Alert}. |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 117 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 118 | @Deprecated |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 119 | public static final int THEME_DEVICE_DEFAULT_LIGHT = 5; |
Craig Stout | 4c0cb8a | 2014-04-04 13:03:10 -0700 | [diff] [blame] | 120 | |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 132 | |
| 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 Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 141 | * @see android.R.styleable#Theme_alertDialogTheme |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 142 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | protected AlertDialog(Context context) { |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 144 | this(context, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 147 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 148 | * 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 Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 163 | * @see android.R.styleable#Theme_alertDialogTheme |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 164 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 165 | protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { |
| 166 | this(context, 0); |
| 167 | |
| 168 | setCancelable(cancelable); |
| 169 | setOnCancelListener(cancelListener); |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 172 | /** |
| 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 Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 196 | * @see android.R.styleable#Theme_alertDialogTheme |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 197 | */ |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 198 | protected AlertDialog(Context context, @StyleRes int themeResId) { |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 199 | this(context, themeResId, true); |
| 200 | } |
| 201 | |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 202 | AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) { |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 203 | super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0, |
| 204 | createContextThemeWrapper); |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 205 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 206 | mWindow.alwaysReadCloseOnTouchAttr(); |
Michael Kwan | cc6e6f0 | 2016-05-16 12:54:57 -0700 | [diff] [blame] | 207 | mAlert = AlertController.create(getContext(), this, getWindow()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Adam Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 210 | static @StyleRes int resolveDialogTheme(Context context, @StyleRes int themeResId) { |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 211 | if (themeResId == THEME_TRADITIONAL) { |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 212 | 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 Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 221 | } else if (ResourceId.isValid(themeResId)) { |
Adam Lesinski | 3601821 | 2017-04-27 18:09:19 -0700 | [diff] [blame] | 222 | // start of real resource IDs. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 223 | return themeResId; |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 224 | } else { |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 225 | final TypedValue outValue = new TypedValue(); |
| 226 | context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true); |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 227 | return outValue.resourceId; |
| 228 | } |
Adam Powell | 2fbf4de6 | 2010-09-30 15:46:46 -0700 | [diff] [blame] | 229 | } |
| 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | /** |
Alan Viverette | a1e6331 | 2013-12-09 18:20:19 -0800 | [diff] [blame] | 232 | * Gets one of the buttons used in the dialog. Returns null if the specified |
Alan Viverette | f34ef74 | 2013-12-11 15:59:53 -0800 | [diff] [blame] | 233 | * button does not exist or the dialog has not yet been fully created (for |
| 234 | * example, via {@link #show()} or {@link #create()}). |
Alan Viverette | a1e6331 | 2013-12-09 18:20:19 -0800 | [diff] [blame] | 235 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | * @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 Viverette | a1e6331 | 2013-12-09 18:20:19 -0800 | [diff] [blame] | 244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | /** |
| 246 | * Gets the list view used in the dialog. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 247 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | * @return The {@link ListView} from the dialog. |
| 249 | */ |
| 250 | public ListView getListView() { |
| 251 | return mAlert.getListView(); |
| 252 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | public void setMessage(CharSequence message) { |
| 268 | mAlert.setMessage(message); |
| 269 | } |
| 270 | |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 271 | /** @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | /** |
| 283 | * Set the view to display in that dialog. |
| 284 | */ |
| 285 | public void setView(View view) { |
| 286 | mAlert.setView(view); |
| 287 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 290 | * Set the view to display in that dialog, specifying the spacing to appear around that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | * 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 Stout | 4c0cb8a | 2014-04-04 13:03:10 -0700 | [diff] [blame] | 305 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | * Set a message to be sent when a button is pressed. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 314 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | * @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 325 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | /** |
| 327 | * Set a listener to be invoked when the positive button of the dialog is pressed. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 328 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | * @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 348 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 369 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | * @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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 412 | public void setIcon(@DrawableRes int resId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | mAlert.setIcon(resId); |
| 414 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | public void setIcon(Drawable icon) { |
| 417 | mAlert.setIcon(icon); |
| 418 | } |
| 419 | |
Adam Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 420 | /** |
| 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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 425 | public void setIconAttribute(@AttrRes int attrId) { |
Adam Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 426 | TypedValue out = new TypedValue(); |
| 427 | mContext.getTheme().resolveAttribute(attrId, out, true); |
| 428 | mAlert.setIcon(out.resourceId); |
| 429 | } |
| 430 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | public void setInverseBackgroundForced(boolean forceInverseBackground) { |
| 432 | mAlert.setInverseBackgroundForced(forceInverseBackground); |
| 433 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | public static class Builder { |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 454 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | private final AlertController.AlertParams P; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 456 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 458 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | */ |
| 467 | public Builder(Context context) { |
Adam Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 468 | this(context, resolveDialogTheme(context, ResourceId.ID_NULL)); |
Martin Nordholts | e429c5e | 2010-06-15 16:17:24 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 472 | * 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 Nordholts | e429c5e | 2010-06-15 16:17:24 +0200 | [diff] [blame] | 496 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 497 | public Builder(Context context, int themeResId) { |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 498 | P = new AlertController.AlertParams(new ContextThemeWrapper( |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 499 | context, resolveDialogTheme(context, themeResId))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 501 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | /** |
Adam Powell | c49c173 | 2010-09-28 16:03:15 -0700 | [diff] [blame] | 503 | * 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 Powell | ccb013f | 2010-10-12 18:42:56 -0700 | [diff] [blame] | 511 | return P.mContext; |
Adam Powell | c49c173 | 2010-09-28 16:03:15 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | * 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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 519 | public Builder setTitle(@StringRes int titleId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | P.mTitle = P.mContext.getText(titleId); |
| 521 | return this; |
| 522 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 523 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 535 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | * |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 546 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | */ |
| 550 | public Builder setCustomTitle(View customTitleView) { |
| 551 | P.mCustomTitleView = customTitleView; |
| 552 | return this; |
| 553 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 554 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | /** |
| 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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 560 | public Builder setMessage(@StringRes int messageId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | P.mMessage = P.mContext.getText(messageId); |
| 562 | return this; |
| 563 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 564 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 574 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | /** |
| 576 | * Set the resource id of the {@link Drawable} to be used in the title. |
Alan Viverette | 5c2d8f7 | 2015-01-05 12:56:45 -0800 | [diff] [blame] | 577 | * <p> |
| 578 | * Takes precedence over values set using {@link #setIcon(Drawable)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | * |
| 580 | * @return This Builder object to allow for chaining of calls to set methods |
| 581 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 582 | public Builder setIcon(@DrawableRes int iconId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | P.mIconId = iconId; |
| 584 | return this; |
| 585 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | /** |
| 588 | * Set the {@link Drawable} to be used in the title. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 589 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | */ |
| 597 | public Builder setIcon(Drawable icon) { |
| 598 | P.mIcon = icon; |
| 599 | return this; |
| 600 | } |
Adam Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 601 | |
| 602 | /** |
Alan Viverette | 5c2d8f7 | 2015-01-05 12:56:45 -0800 | [diff] [blame] | 603 | * 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 Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 608 | * |
| 609 | * @param attrId ID of a theme attribute that points to a drawable resource. |
| 610 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 611 | public Builder setIconAttribute(@AttrRes int attrId) { |
Adam Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 612 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | /** |
| 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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 625 | public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | P.mPositiveButtonText = P.mContext.getText(textId); |
| 627 | P.mPositiveButtonListener = listener; |
| 628 | return this; |
| 629 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 630 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 643 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | /** |
| 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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 651 | public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | P.mNegativeButtonText = P.mContext.getText(textId); |
| 653 | P.mNegativeButtonListener = listener; |
| 654 | return this; |
| 655 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 669 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | /** |
| 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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 677 | public Builder setNeutralButton(@StringRes int textId, final OnClickListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | P.mNeutralButtonText = P.mContext.getText(textId); |
| 679 | P.mNeutralButtonListener = listener; |
| 680 | return this; |
| 681 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 695 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | /** |
Brad Fitzpatrick | 69ea4e1 | 2011-01-05 11:13:40 -0800 | [diff] [blame] | 697 | * Sets whether the dialog is cancelable or not. Default is true. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | * |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | /** |
| 707 | * Sets the callback that will be called if the dialog is canceled. |
Adam Powell | 7f02dc5 | 2012-08-27 13:35:16 -0700 | [diff] [blame] | 708 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | * @see #setCancelable(boolean) |
Adam Powell | 7f02dc5 | 2012-08-27 13:35:16 -0700 | [diff] [blame] | 715 | * @see #setOnDismissListener(android.content.DialogInterface.OnDismissListener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | * |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 723 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | /** |
Adam Powell | 7f02dc5 | 2012-08-27 13:35:16 -0700 | [diff] [blame] | 725 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | * 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 743 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | /** |
| 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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 750 | public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | P.mItems = P.mContext.getResources().getTextArray(itemsId); |
| 752 | P.mOnClickListener = listener; |
| 753 | return this; |
| 754 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 755 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 767 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 772 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | * @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 783 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 788 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | * @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 803 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 811 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | * @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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 822 | public Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 837 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | * @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 848 | public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | final OnMultiChoiceClickListener listener) { |
| 850 | P.mItems = items; |
| 851 | P.mOnCheckboxClickListener = listener; |
| 852 | P.mCheckedItems = checkedItems; |
| 853 | P.mIsMultiChoice = true; |
| 854 | return this; |
| 855 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 856 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 863 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | * @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 876 | public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 885 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 892 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | * @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 Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 901 | public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 915 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | * @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 926 | public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 935 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 941 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | * @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 956 | } |
| 957 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | /** |
| 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 963 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | * @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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 979 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | /** |
| 981 | * Sets a listener to be invoked when an item in the list is selected. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | * |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 983 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | */ |
| 987 | public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) { |
| 988 | P.mOnItemSelectedListener = listener; |
| 989 | return this; |
| 990 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 991 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | /** |
Alan Viverette | ec18670 | 2013-12-05 11:10:31 -0800 | [diff] [blame] | 993 | * 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 997 | * @return this Builder object to allow for chaining of calls to set |
Alan Viverette | ec18670 | 2013-12-05 11:10:31 -0800 | [diff] [blame] | 998 | * 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 Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1008 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | * |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1017 | * @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | */ |
| 1021 | public Builder setView(View view) { |
| 1022 | P.mView = view; |
Alan Viverette | ec18670 | 2013-12-05 11:10:31 -0800 | [diff] [blame] | 1023 | P.mViewLayoutResId = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | P.mViewSpacingSpecified = false; |
| 1025 | return this; |
| 1026 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1027 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1029 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | * methods |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1050 | * |
| 1051 | * @hide Remove once the framework usages have been replaced. |
| 1052 | * @deprecated Set the padding on the view itself. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1054 | @Deprecated |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 1055 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop, |
| 1057 | int viewSpacingRight, int viewSpacingBottom) { |
| 1058 | P.mView = view; |
Alan Viverette | ec18670 | 2013-12-05 11:10:31 -0800 | [diff] [blame] | 1059 | P.mViewLayoutResId = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | P.mViewSpacingSpecified = true; |
| 1061 | P.mViewSpacingLeft = viewSpacingLeft; |
| 1062 | P.mViewSpacingTop = viewSpacingTop; |
| 1063 | P.mViewSpacingRight = viewSpacingRight; |
| 1064 | P.mViewSpacingBottom = viewSpacingBottom; |
| 1065 | return this; |
| 1066 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1067 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1069 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1078 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | public Builder setInverseBackgroundForced(boolean useInverseBackground) { |
| 1080 | P.mForceInverseBackground = useInverseBackground; |
| 1081 | return this; |
| 1082 | } |
Romain Guy | 9c802c1 | 2009-03-25 15:07:31 -0700 | [diff] [blame] | 1083 | |
| 1084 | /** |
| 1085 | * @hide |
| 1086 | */ |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 1087 | @UnsupportedAppUsage |
Romain Guy | 9c802c1 | 2009-03-25 15:07:31 -0700 | [diff] [blame] | 1088 | public Builder setRecycleOnMeasureEnabled(boolean enabled) { |
| 1089 | P.mRecycleOnMeasure = enabled; |
| 1090 | return this; |
| 1091 | } |
| 1092 | |
| 1093 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1095 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | */ |
| 1102 | public AlertDialog create() { |
Alan Viverette | 93d9e26 | 2015-08-11 15:02:42 -0400 | [diff] [blame] | 1103 | // Context has already been wrapped with the appropriate theme. |
| 1104 | final AlertDialog dialog = new AlertDialog(P.mContext, 0, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | P.apply(dialog.mAlert); |
Adam Powell | fbca769 | 2011-06-22 21:29:19 -0700 | [diff] [blame] | 1106 | dialog.setCancelable(P.mCancelable); |
| 1107 | if (P.mCancelable) { |
| 1108 | dialog.setCanceledOnTouchOutside(true); |
| 1109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | dialog.setOnCancelListener(P.mOnCancelListener); |
Adam Powell | 7f02dc5 | 2012-08-27 13:35:16 -0700 | [diff] [blame] | 1111 | dialog.setOnDismissListener(P.mOnDismissListener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | if (P.mOnKeyListener != null) { |
| 1113 | dialog.setOnKeyListener(P.mOnKeyListener); |
| 1114 | } |
| 1115 | return dialog; |
| 1116 | } |
| 1117 | |
| 1118 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1119 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | */ |
| 1128 | public AlertDialog show() { |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1129 | final AlertDialog dialog = create(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | dialog.show(); |
| 1131 | return dialog; |
| 1132 | } |
| 1133 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | } |