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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.DialogInterface; |
Adam Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 26 | import android.content.res.ResourceId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.database.Cursor; |
| 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.os.Bundle; |
| 30 | import android.os.Message; |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 31 | import android.text.Layout; |
| 32 | import android.text.method.MovementMethod; |
Adam Powell | 2fbf4de6 | 2010-09-30 15:46:46 -0700 | [diff] [blame] | 33 | import android.util.TypedValue; |
Adam Powell | c49c173 | 2010-09-28 16:03:15 -0700 | [diff] [blame] | 34 | import android.view.ContextThemeWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.view.KeyEvent; |
| 36 | import android.view.View; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.widget.AdapterView; |
| 38 | import android.widget.Button; |
| 39 | import android.widget.ListAdapter; |
| 40 | import android.widget.ListView; |
| 41 | |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 42 | import com.android.internal.R; |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 43 | import com.android.internal.app.AlertController; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | /** |
| 46 | * A subclass of Dialog that can display one, two or three buttons. If you only want to |
| 47 | * display a String in this dialog box, use the setMessage() method. If you |
Adam Powell | c900687 | 2010-03-18 13:44:45 -0700 | [diff] [blame] | 48 | * 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] | 49 | * and add your view to it: |
| 50 | * |
| 51 | * <pre> |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 52 | * FrameLayout fl = findViewById(android.R.id.custom); |
Adam Powell | c900687 | 2010-03-18 13:44:45 -0700 | [diff] [blame] | 53 | * fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | * </pre> |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 55 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | * <p>The AlertDialog class takes care of automatically setting |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 57 | * {@link android.view.WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | * WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} for you based on whether |
| 59 | * any views in the dialog return true from {@link View#onCheckIsTextEditor() |
| 60 | * View.onCheckIsTextEditor()}. Generally you want this set for a Dialog |
| 61 | * without text editors, so that it will be placed on top of the current |
| 62 | * input method UI. You can modify this behavior by forcing the flag to your |
| 63 | * desired mode after calling {@link #onCreate}. |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 64 | * |
| 65 | * <div class="special reference"> |
| 66 | * <h3>Developer Guides</h3> |
| 67 | * <p>For more information about creating dialogs, read the |
| 68 | * <a href="{@docRoot}guide/topics/ui/dialogs.html">Dialogs</a> developer guide.</p> |
| 69 | * </div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | */ |
| 71 | public class AlertDialog extends Dialog implements DialogInterface { |
| 72 | private AlertController mAlert; |
| 73 | |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
| 76 | * the traditional (pre-Holo) alert dialog theme. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 77 | * |
| 78 | * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}. |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 79 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 80 | @Deprecated |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 81 | public static final int THEME_TRADITIONAL = 1; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 82 | |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
| 85 | * the holographic alert theme with a dark background. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 86 | * |
| 87 | * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}. |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 88 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 89 | @Deprecated |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 90 | public static final int THEME_HOLO_DARK = 2; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 91 | |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
| 94 | * the holographic alert theme with a light background. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 95 | * |
| 96 | * @deprecated Use {@link android.R.style#Theme_Material_Light_Dialog_Alert}. |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 97 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 98 | @Deprecated |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 99 | public static final int THEME_HOLO_LIGHT = 3; |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
| 103 | * the device's default alert theme with a dark background. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 104 | * |
| 105 | * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Dialog_Alert}. |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 106 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 107 | @Deprecated |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 108 | public static final int THEME_DEVICE_DEFAULT_DARK = 4; |
| 109 | |
| 110 | /** |
| 111 | * Special theme constant for {@link #AlertDialog(Context, int)}: use |
Scott Kennedy | 7ed189e | 2013-01-11 22:31:43 -0800 | [diff] [blame] | 112 | * the device's default alert theme with a light background. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 113 | * |
| 114 | * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Light_Dialog_Alert}. |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 115 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 116 | @Deprecated |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 117 | public static final int THEME_DEVICE_DEFAULT_LIGHT = 5; |
Craig Stout | 4c0cb8a | 2014-04-04 13:03:10 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * No layout hint. |
| 121 | * @hide |
| 122 | */ |
| 123 | public static final int LAYOUT_HINT_NONE = 0; |
| 124 | |
| 125 | /** |
| 126 | * Hint layout to the side. |
| 127 | * @hide |
| 128 | */ |
| 129 | public static final int LAYOUT_HINT_SIDE = 1; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * Creates an alert dialog that uses the default alert dialog theme. |
| 133 | * <p> |
| 134 | * The default alert dialog theme is defined by |
| 135 | * {@link android.R.attr#alertDialogTheme} within the parent |
| 136 | * {@code context}'s theme. |
| 137 | * |
| 138 | * @param context the parent context |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 139 | * @see android.R.styleable#Theme_alertDialogTheme |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 140 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | protected AlertDialog(Context context) { |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 142 | this(context, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 145 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 146 | * Creates an alert dialog that uses the default alert dialog theme and a |
| 147 | * custom cancel listener. |
| 148 | * <p> |
| 149 | * This is functionally identical to: |
| 150 | * <pre> |
| 151 | * AlertDialog dialog = new AlertDialog(context); |
| 152 | * alertDialog.setCancelable(cancelable); |
| 153 | * alertDialog.setOnCancelListener(cancelListener); |
| 154 | * </pre> |
| 155 | * <p> |
| 156 | * The default alert dialog theme is defined by |
| 157 | * {@link android.R.attr#alertDialogTheme} within the parent |
| 158 | * {@code context}'s theme. |
| 159 | * |
| 160 | * @param context the parent context |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 161 | * @see android.R.styleable#Theme_alertDialogTheme |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 162 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 163 | protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { |
| 164 | this(context, 0); |
| 165 | |
| 166 | setCancelable(cancelable); |
| 167 | setOnCancelListener(cancelListener); |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 170 | /** |
| 171 | * Creates an alert dialog that uses an explicit theme resource. |
| 172 | * <p> |
| 173 | * The specified theme resource ({@code themeResId}) is applied on top of |
| 174 | * the parent {@code context}'s theme. It may be specified as a style |
| 175 | * resource containing a fully-populated theme, such as |
| 176 | * {@link android.R.style#Theme_Material_Dialog}, to replace all attributes |
| 177 | * in the parent {@code context}'s theme including primary and accent |
| 178 | * colors. |
| 179 | * <p> |
| 180 | * To preserve attributes such as primary and accent colors, the |
| 181 | * {@code themeResId} may instead be specified as an overlay theme such as |
| 182 | * {@link android.R.style#ThemeOverlay_Material_Dialog}. This will override |
| 183 | * only the window attributes necessary to style the alert window as a |
| 184 | * dialog. |
| 185 | * <p> |
| 186 | * Alternatively, the {@code themeResId} may be specified as {@code 0} to |
| 187 | * use the parent {@code context}'s resolved value for |
| 188 | * {@link android.R.attr#alertDialogTheme}. |
| 189 | * |
| 190 | * @param context the parent context |
| 191 | * @param themeResId the resource ID of the theme against which to inflate |
| 192 | * this dialog, or {@code 0} to use the parent |
| 193 | * {@code context}'s default alert dialog theme |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 194 | * @see android.R.styleable#Theme_alertDialogTheme |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 195 | */ |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 196 | protected AlertDialog(Context context, @StyleRes int themeResId) { |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 197 | this(context, themeResId, true); |
| 198 | } |
| 199 | |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 200 | AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) { |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 201 | super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0, |
| 202 | createContextThemeWrapper); |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 203 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 204 | mWindow.alwaysReadCloseOnTouchAttr(); |
Michael Kwan | cc6e6f0 | 2016-05-16 12:54:57 -0700 | [diff] [blame] | 205 | mAlert = AlertController.create(getContext(), this, getWindow()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Adam Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 208 | static @StyleRes int resolveDialogTheme(Context context, @StyleRes int themeResId) { |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 209 | if (themeResId == THEME_TRADITIONAL) { |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 210 | return R.style.Theme_Dialog_Alert; |
| 211 | } else if (themeResId == THEME_HOLO_DARK) { |
| 212 | return R.style.Theme_Holo_Dialog_Alert; |
| 213 | } else if (themeResId == THEME_HOLO_LIGHT) { |
| 214 | return R.style.Theme_Holo_Light_Dialog_Alert; |
| 215 | } else if (themeResId == THEME_DEVICE_DEFAULT_DARK) { |
| 216 | return R.style.Theme_DeviceDefault_Dialog_Alert; |
| 217 | } else if (themeResId == THEME_DEVICE_DEFAULT_LIGHT) { |
| 218 | return R.style.Theme_DeviceDefault_Light_Dialog_Alert; |
Adam Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 219 | } else if (ResourceId.isValid(themeResId)) { |
Adam Lesinski | 3601821 | 2017-04-27 18:09:19 -0700 | [diff] [blame] | 220 | // start of real resource IDs. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 221 | return themeResId; |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 222 | } else { |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 223 | final TypedValue outValue = new TypedValue(); |
| 224 | context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true); |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 225 | return outValue.resourceId; |
| 226 | } |
Adam Powell | 2fbf4de6 | 2010-09-30 15:46:46 -0700 | [diff] [blame] | 227 | } |
| 228 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | /** |
Alan Viverette | a1e6331 | 2013-12-09 18:20:19 -0800 | [diff] [blame] | 230 | * 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] | 231 | * button does not exist or the dialog has not yet been fully created (for |
| 232 | * example, via {@link #show()} or {@link #create()}). |
Alan Viverette | a1e6331 | 2013-12-09 18:20:19 -0800 | [diff] [blame] | 233 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | * @param whichButton The identifier of the button that should be returned. |
| 235 | * For example, this can be |
| 236 | * {@link DialogInterface#BUTTON_POSITIVE}. |
| 237 | * @return The button from the dialog, or null if a button does not exist. |
| 238 | */ |
| 239 | public Button getButton(int whichButton) { |
| 240 | return mAlert.getButton(whichButton); |
| 241 | } |
Alan Viverette | a1e6331 | 2013-12-09 18:20:19 -0800 | [diff] [blame] | 242 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | /** |
| 244 | * Gets the list view used in the dialog. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 245 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | * @return The {@link ListView} from the dialog. |
| 247 | */ |
| 248 | public ListView getListView() { |
| 249 | return mAlert.getListView(); |
| 250 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | @Override |
| 253 | public void setTitle(CharSequence title) { |
| 254 | super.setTitle(title); |
| 255 | mAlert.setTitle(title); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @see Builder#setCustomTitle(View) |
| 260 | */ |
| 261 | public void setCustomTitle(View customTitleView) { |
| 262 | mAlert.setCustomTitle(customTitleView); |
| 263 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 264 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | public void setMessage(CharSequence message) { |
| 266 | mAlert.setMessage(message); |
| 267 | } |
| 268 | |
Makoto Onuki | 778ce66 | 2018-04-20 14:04:50 -0700 | [diff] [blame] | 269 | /** @hide */ |
| 270 | public void setMessageMovementMethod(MovementMethod movementMethod) { |
| 271 | mAlert.setMessageMovementMethod(movementMethod); |
| 272 | } |
| 273 | |
| 274 | /** @hide */ |
| 275 | public void setMessageHyphenationFrequency( |
| 276 | @Layout.HyphenationFrequency int hyphenationFrequency) { |
| 277 | mAlert.setMessageHyphenationFrequency(hyphenationFrequency); |
| 278 | } |
| 279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | /** |
| 281 | * Set the view to display in that dialog. |
| 282 | */ |
| 283 | public void setView(View view) { |
| 284 | mAlert.setView(view); |
| 285 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 286 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 288 | * 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] | 289 | * view. |
| 290 | * |
| 291 | * @param view The view to show in the content area of the dialog |
| 292 | * @param viewSpacingLeft Extra space to appear to the left of {@code view} |
| 293 | * @param viewSpacingTop Extra space to appear above {@code view} |
| 294 | * @param viewSpacingRight Extra space to appear to the right of {@code view} |
| 295 | * @param viewSpacingBottom Extra space to appear below {@code view} |
| 296 | */ |
| 297 | public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, |
| 298 | int viewSpacingBottom) { |
| 299 | mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom); |
| 300 | } |
| 301 | |
| 302 | /** |
Craig Stout | 4c0cb8a | 2014-04-04 13:03:10 -0700 | [diff] [blame] | 303 | * Internal api to allow hinting for the best button panel layout. |
| 304 | * @hide |
| 305 | */ |
| 306 | void setButtonPanelLayoutHint(int layoutHint) { |
| 307 | mAlert.setButtonPanelLayoutHint(layoutHint); |
| 308 | } |
| 309 | |
| 310 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | * Set a message to be sent when a button is pressed. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 312 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | * @param whichButton Which button to set the message for, can be one of |
| 314 | * {@link DialogInterface#BUTTON_POSITIVE}, |
| 315 | * {@link DialogInterface#BUTTON_NEGATIVE}, or |
| 316 | * {@link DialogInterface#BUTTON_NEUTRAL} |
| 317 | * @param text The text to display in positive button. |
| 318 | * @param msg The {@link Message} to be sent when clicked. |
| 319 | */ |
| 320 | public void setButton(int whichButton, CharSequence text, Message msg) { |
| 321 | mAlert.setButton(whichButton, text, null, msg); |
| 322 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | /** |
| 325 | * 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] | 326 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | * @param whichButton Which button to set the listener on, can be one of |
| 328 | * {@link DialogInterface#BUTTON_POSITIVE}, |
| 329 | * {@link DialogInterface#BUTTON_NEGATIVE}, or |
| 330 | * {@link DialogInterface#BUTTON_NEUTRAL} |
| 331 | * @param text The text to display in positive button. |
| 332 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 333 | */ |
| 334 | public void setButton(int whichButton, CharSequence text, OnClickListener listener) { |
| 335 | mAlert.setButton(whichButton, text, listener, null); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * @deprecated Use {@link #setButton(int, CharSequence, Message)} with |
| 340 | * {@link DialogInterface#BUTTON_POSITIVE}. |
| 341 | */ |
| 342 | @Deprecated |
| 343 | public void setButton(CharSequence text, Message msg) { |
| 344 | setButton(BUTTON_POSITIVE, text, msg); |
| 345 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | /** |
| 348 | * @deprecated Use {@link #setButton(int, CharSequence, Message)} with |
| 349 | * {@link DialogInterface#BUTTON_NEGATIVE}. |
| 350 | */ |
| 351 | @Deprecated |
| 352 | public void setButton2(CharSequence text, Message msg) { |
| 353 | setButton(BUTTON_NEGATIVE, text, msg); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * @deprecated Use {@link #setButton(int, CharSequence, Message)} with |
| 358 | * {@link DialogInterface#BUTTON_NEUTRAL}. |
| 359 | */ |
| 360 | @Deprecated |
| 361 | public void setButton3(CharSequence text, Message msg) { |
| 362 | setButton(BUTTON_NEUTRAL, text, msg); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Set a listener to be invoked when button 1 of the dialog is pressed. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 367 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | * @param text The text to display in button 1. |
| 369 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 370 | * @deprecated Use |
| 371 | * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)} |
| 372 | * with {@link DialogInterface#BUTTON_POSITIVE} |
| 373 | */ |
| 374 | @Deprecated |
| 375 | public void setButton(CharSequence text, final OnClickListener listener) { |
| 376 | setButton(BUTTON_POSITIVE, text, listener); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Set a listener to be invoked when button 2 of the dialog is pressed. |
| 381 | * @param text The text to display in button 2. |
| 382 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 383 | * @deprecated Use |
| 384 | * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)} |
| 385 | * with {@link DialogInterface#BUTTON_NEGATIVE} |
| 386 | */ |
| 387 | @Deprecated |
| 388 | public void setButton2(CharSequence text, final OnClickListener listener) { |
| 389 | setButton(BUTTON_NEGATIVE, text, listener); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Set a listener to be invoked when button 3 of the dialog is pressed. |
| 394 | * @param text The text to display in button 3. |
| 395 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 396 | * @deprecated Use |
| 397 | * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)} |
| 398 | * with {@link DialogInterface#BUTTON_POSITIVE} |
| 399 | */ |
| 400 | @Deprecated |
| 401 | public void setButton3(CharSequence text, final OnClickListener listener) { |
| 402 | setButton(BUTTON_NEUTRAL, text, listener); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Set resId to 0 if you don't want an icon. |
| 407 | * @param resId the resourceId of the drawable to use as the icon or 0 |
| 408 | * if you don't want an icon. |
| 409 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 410 | public void setIcon(@DrawableRes int resId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | mAlert.setIcon(resId); |
| 412 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 413 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | public void setIcon(Drawable icon) { |
| 415 | mAlert.setIcon(icon); |
| 416 | } |
| 417 | |
Adam Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 418 | /** |
| 419 | * Set an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon |
| 420 | * |
| 421 | * @param attrId ID of a theme attribute that points to a drawable resource. |
| 422 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 423 | public void setIconAttribute(@AttrRes int attrId) { |
Adam Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 424 | TypedValue out = new TypedValue(); |
| 425 | mContext.getTheme().resolveAttribute(attrId, out, true); |
| 426 | mAlert.setIcon(out.resourceId); |
| 427 | } |
| 428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | public void setInverseBackgroundForced(boolean forceInverseBackground) { |
| 430 | mAlert.setInverseBackgroundForced(forceInverseBackground); |
| 431 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 432 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | @Override |
| 434 | protected void onCreate(Bundle savedInstanceState) { |
| 435 | super.onCreate(savedInstanceState); |
| 436 | mAlert.installContent(); |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 441 | if (mAlert.onKeyDown(keyCode, event)) return true; |
| 442 | return super.onKeyDown(keyCode, event); |
| 443 | } |
| 444 | |
| 445 | @Override |
| 446 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 447 | if (mAlert.onKeyUp(keyCode, event)) return true; |
| 448 | return super.onKeyUp(keyCode, event); |
| 449 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | public static class Builder { |
| 452 | private final AlertController.AlertParams P; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 453 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 455 | * Creates a builder for an alert dialog that uses the default alert |
| 456 | * dialog theme. |
| 457 | * <p> |
| 458 | * The default alert dialog theme is defined by |
| 459 | * {@link android.R.attr#alertDialogTheme} within the parent |
| 460 | * {@code context}'s theme. |
| 461 | * |
| 462 | * @param context the parent context |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | */ |
| 464 | public Builder(Context context) { |
Adam Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 465 | this(context, resolveDialogTheme(context, ResourceId.ID_NULL)); |
Martin Nordholts | e429c5e | 2010-06-15 16:17:24 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 469 | * Creates a builder for an alert dialog that uses an explicit theme |
| 470 | * resource. |
| 471 | * <p> |
| 472 | * The specified theme resource ({@code themeResId}) is applied on top |
| 473 | * of the parent {@code context}'s theme. It may be specified as a |
| 474 | * style resource containing a fully-populated theme, such as |
| 475 | * {@link android.R.style#Theme_Material_Dialog}, to replace all |
| 476 | * attributes in the parent {@code context}'s theme including primary |
| 477 | * and accent colors. |
| 478 | * <p> |
| 479 | * To preserve attributes such as primary and accent colors, the |
| 480 | * {@code themeResId} may instead be specified as an overlay theme such |
| 481 | * as {@link android.R.style#ThemeOverlay_Material_Dialog}. This will |
| 482 | * override only the window attributes necessary to style the alert |
| 483 | * window as a dialog. |
| 484 | * <p> |
| 485 | * Alternatively, the {@code themeResId} may be specified as {@code 0} |
| 486 | * to use the parent {@code context}'s resolved value for |
| 487 | * {@link android.R.attr#alertDialogTheme}. |
| 488 | * |
| 489 | * @param context the parent context |
| 490 | * @param themeResId the resource ID of the theme against which to inflate |
| 491 | * this dialog, or {@code 0} to use the parent |
| 492 | * {@code context}'s default alert dialog theme |
Martin Nordholts | e429c5e | 2010-06-15 16:17:24 +0200 | [diff] [blame] | 493 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 494 | public Builder(Context context, int themeResId) { |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 495 | P = new AlertController.AlertParams(new ContextThemeWrapper( |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 496 | context, resolveDialogTheme(context, themeResId))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 498 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | /** |
Adam Powell | c49c173 | 2010-09-28 16:03:15 -0700 | [diff] [blame] | 500 | * Returns a {@link Context} with the appropriate theme for dialogs created by this Builder. |
| 501 | * Applications should use this Context for obtaining LayoutInflaters for inflating views |
| 502 | * that will be used in the resulting dialogs, as it will cause views to be inflated with |
| 503 | * the correct theme. |
| 504 | * |
| 505 | * @return A Context for built Dialogs. |
| 506 | */ |
| 507 | public Context getContext() { |
Adam Powell | ccb013f | 2010-10-12 18:42:56 -0700 | [diff] [blame] | 508 | return P.mContext; |
Adam Powell | c49c173 | 2010-09-28 16:03:15 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | * Set the title using the given resource id. |
| 513 | * |
| 514 | * @return This Builder object to allow for chaining of calls to set methods |
| 515 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 516 | public Builder setTitle(@StringRes int titleId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | P.mTitle = P.mContext.getText(titleId); |
| 518 | return this; |
| 519 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 520 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | /** |
| 522 | * Set the title displayed in the {@link Dialog}. |
| 523 | * |
| 524 | * @return This Builder object to allow for chaining of calls to set methods |
| 525 | */ |
| 526 | public Builder setTitle(CharSequence title) { |
| 527 | P.mTitle = title; |
| 528 | return this; |
| 529 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 532 | * Set the title using the custom view {@code customTitleView}. |
| 533 | * <p> |
| 534 | * The methods {@link #setTitle(int)} and {@link #setIcon(int)} should |
| 535 | * be sufficient for most titles, but this is provided if the title |
| 536 | * needs more customization. Using this will replace the title and icon |
| 537 | * set via the other methods. |
| 538 | * <p> |
| 539 | * <strong>Note:</strong> To ensure consistent styling, the custom view |
| 540 | * should be inflated or constructed using the alert dialog's themed |
| 541 | * context obtained via {@link #getContext()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | * |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 543 | * @param customTitleView the custom view to use as the title |
| 544 | * @return this Builder object to allow for chaining of calls to set |
| 545 | * methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | */ |
| 547 | public Builder setCustomTitle(View customTitleView) { |
| 548 | P.mCustomTitleView = customTitleView; |
| 549 | return this; |
| 550 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 551 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | /** |
| 553 | * Set the message to display using the given resource id. |
| 554 | * |
| 555 | * @return This Builder object to allow for chaining of calls to set methods |
| 556 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 557 | public Builder setMessage(@StringRes int messageId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | P.mMessage = P.mContext.getText(messageId); |
| 559 | return this; |
| 560 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 561 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | /** |
| 563 | * Set the message to display. |
| 564 | * |
| 565 | * @return This Builder object to allow for chaining of calls to set methods |
| 566 | */ |
| 567 | public Builder setMessage(CharSequence message) { |
| 568 | P.mMessage = message; |
| 569 | return this; |
| 570 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 571 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | /** |
| 573 | * 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] | 574 | * <p> |
| 575 | * Takes precedence over values set using {@link #setIcon(Drawable)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | * |
| 577 | * @return This Builder object to allow for chaining of calls to set methods |
| 578 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 579 | public Builder setIcon(@DrawableRes int iconId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | P.mIconId = iconId; |
| 581 | return this; |
| 582 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | /** |
| 585 | * Set the {@link Drawable} to be used in the title. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 586 | * <p> |
| 587 | * <strong>Note:</strong> To ensure consistent styling, the drawable |
| 588 | * should be inflated or constructed using the alert dialog's themed |
| 589 | * context obtained via {@link #getContext()}. |
| 590 | * |
| 591 | * @return this Builder object to allow for chaining of calls to set |
| 592 | * methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | */ |
| 594 | public Builder setIcon(Drawable icon) { |
| 595 | P.mIcon = icon; |
| 596 | return this; |
| 597 | } |
Adam Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 598 | |
| 599 | /** |
Alan Viverette | 5c2d8f7 | 2015-01-05 12:56:45 -0800 | [diff] [blame] | 600 | * Set an icon as supplied by a theme attribute. e.g. |
| 601 | * {@link android.R.attr#alertDialogIcon}. |
| 602 | * <p> |
| 603 | * Takes precedence over values set using {@link #setIcon(int)} or |
| 604 | * {@link #setIcon(Drawable)}. |
Adam Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 605 | * |
| 606 | * @param attrId ID of a theme attribute that points to a drawable resource. |
| 607 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 608 | public Builder setIconAttribute(@AttrRes int attrId) { |
Adam Powell | 947f782 | 2011-01-07 22:30:48 -0800 | [diff] [blame] | 609 | TypedValue out = new TypedValue(); |
| 610 | P.mContext.getTheme().resolveAttribute(attrId, out, true); |
| 611 | P.mIconId = out.resourceId; |
| 612 | return this; |
| 613 | } |
| 614 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | /** |
| 616 | * Set a listener to be invoked when the positive button of the dialog is pressed. |
| 617 | * @param textId The resource id of the text to display in the positive button |
| 618 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 619 | * |
| 620 | * @return This Builder object to allow for chaining of calls to set methods |
| 621 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 622 | public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | P.mPositiveButtonText = P.mContext.getText(textId); |
| 624 | P.mPositiveButtonListener = listener; |
| 625 | return this; |
| 626 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 627 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | /** |
| 629 | * Set a listener to be invoked when the positive button of the dialog is pressed. |
| 630 | * @param text The text to display in the positive button |
| 631 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 632 | * |
| 633 | * @return This Builder object to allow for chaining of calls to set methods |
| 634 | */ |
| 635 | public Builder setPositiveButton(CharSequence text, final OnClickListener listener) { |
| 636 | P.mPositiveButtonText = text; |
| 637 | P.mPositiveButtonListener = listener; |
| 638 | return this; |
| 639 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 640 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | /** |
| 642 | * Set a listener to be invoked when the negative button of the dialog is pressed. |
| 643 | * @param textId The resource id of the text to display in the negative button |
| 644 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 645 | * |
| 646 | * @return This Builder object to allow for chaining of calls to set methods |
| 647 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 648 | public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | P.mNegativeButtonText = P.mContext.getText(textId); |
| 650 | P.mNegativeButtonListener = listener; |
| 651 | return this; |
| 652 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | /** |
| 655 | * Set a listener to be invoked when the negative button of the dialog is pressed. |
| 656 | * @param text The text to display in the negative button |
| 657 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 658 | * |
| 659 | * @return This Builder object to allow for chaining of calls to set methods |
| 660 | */ |
| 661 | public Builder setNegativeButton(CharSequence text, final OnClickListener listener) { |
| 662 | P.mNegativeButtonText = text; |
| 663 | P.mNegativeButtonListener = listener; |
| 664 | return this; |
| 665 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 666 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | /** |
| 668 | * Set a listener to be invoked when the neutral button of the dialog is pressed. |
| 669 | * @param textId The resource id of the text to display in the neutral button |
| 670 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 671 | * |
| 672 | * @return This Builder object to allow for chaining of calls to set methods |
| 673 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 674 | public Builder setNeutralButton(@StringRes int textId, final OnClickListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | P.mNeutralButtonText = P.mContext.getText(textId); |
| 676 | P.mNeutralButtonListener = listener; |
| 677 | return this; |
| 678 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 679 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | /** |
| 681 | * Set a listener to be invoked when the neutral button of the dialog is pressed. |
| 682 | * @param text The text to display in the neutral button |
| 683 | * @param listener The {@link DialogInterface.OnClickListener} to use. |
| 684 | * |
| 685 | * @return This Builder object to allow for chaining of calls to set methods |
| 686 | */ |
| 687 | public Builder setNeutralButton(CharSequence text, final OnClickListener listener) { |
| 688 | P.mNeutralButtonText = text; |
| 689 | P.mNeutralButtonListener = listener; |
| 690 | return this; |
| 691 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 692 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | /** |
Brad Fitzpatrick | 69ea4e1 | 2011-01-05 11:13:40 -0800 | [diff] [blame] | 694 | * 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] | 695 | * |
| 696 | * @return This Builder object to allow for chaining of calls to set methods |
| 697 | */ |
| 698 | public Builder setCancelable(boolean cancelable) { |
| 699 | P.mCancelable = cancelable; |
| 700 | return this; |
| 701 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 702 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | /** |
| 704 | * Sets the callback that will be called if the dialog is canceled. |
Adam Powell | 7f02dc5 | 2012-08-27 13:35:16 -0700 | [diff] [blame] | 705 | * |
| 706 | * <p>Even in a cancelable dialog, the dialog may be dismissed for reasons other than |
| 707 | * being canceled or one of the supplied choices being selected. |
| 708 | * If you are interested in listening for all cases where the dialog is dismissed |
| 709 | * and not just when it is canceled, see |
| 710 | * {@link #setOnDismissListener(android.content.DialogInterface.OnDismissListener) setOnDismissListener}.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | * @see #setCancelable(boolean) |
Adam Powell | 7f02dc5 | 2012-08-27 13:35:16 -0700 | [diff] [blame] | 712 | * @see #setOnDismissListener(android.content.DialogInterface.OnDismissListener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | * |
| 714 | * @return This Builder object to allow for chaining of calls to set methods |
| 715 | */ |
| 716 | public Builder setOnCancelListener(OnCancelListener onCancelListener) { |
| 717 | P.mOnCancelListener = onCancelListener; |
| 718 | return this; |
| 719 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 720 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | /** |
Adam Powell | 7f02dc5 | 2012-08-27 13:35:16 -0700 | [diff] [blame] | 722 | * Sets the callback that will be called when the dialog is dismissed for any reason. |
| 723 | * |
| 724 | * @return This Builder object to allow for chaining of calls to set methods |
| 725 | */ |
| 726 | public Builder setOnDismissListener(OnDismissListener onDismissListener) { |
| 727 | P.mOnDismissListener = onDismissListener; |
| 728 | return this; |
| 729 | } |
| 730 | |
| 731 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | * Sets the callback that will be called if a key is dispatched to the dialog. |
| 733 | * |
| 734 | * @return This Builder object to allow for chaining of calls to set methods |
| 735 | */ |
| 736 | public Builder setOnKeyListener(OnKeyListener onKeyListener) { |
| 737 | P.mOnKeyListener = onKeyListener; |
| 738 | return this; |
| 739 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | /** |
| 742 | * Set a list of items to be displayed in the dialog as the content, you will be notified of the |
| 743 | * selected item via the supplied listener. This should be an array type i.e. R.array.foo |
| 744 | * |
| 745 | * @return This Builder object to allow for chaining of calls to set methods |
| 746 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 747 | public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | P.mItems = P.mContext.getResources().getTextArray(itemsId); |
| 749 | P.mOnClickListener = listener; |
| 750 | return this; |
| 751 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 752 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | /** |
| 754 | * Set a list of items to be displayed in the dialog as the content, you will be notified of the |
| 755 | * selected item via the supplied listener. |
| 756 | * |
| 757 | * @return This Builder object to allow for chaining of calls to set methods |
| 758 | */ |
| 759 | public Builder setItems(CharSequence[] items, final OnClickListener listener) { |
| 760 | P.mItems = items; |
| 761 | P.mOnClickListener = listener; |
| 762 | return this; |
| 763 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | /** |
| 766 | * Set a list of items, which are supplied by the given {@link ListAdapter}, to be |
| 767 | * displayed in the dialog as the content, you will be notified of the |
| 768 | * selected item via the supplied listener. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 769 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | * @param adapter The {@link ListAdapter} to supply the list of items |
| 771 | * @param listener The listener that will be called when an item is clicked. |
| 772 | * |
| 773 | * @return This Builder object to allow for chaining of calls to set methods |
| 774 | */ |
| 775 | public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) { |
| 776 | P.mAdapter = adapter; |
| 777 | P.mOnClickListener = listener; |
| 778 | return this; |
| 779 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 780 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | /** |
| 782 | * Set a list of items, which are supplied by the given {@link Cursor}, to be |
| 783 | * displayed in the dialog as the content, you will be notified of the |
| 784 | * selected item via the supplied listener. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 785 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | * @param cursor The {@link Cursor} to supply the list of items |
| 787 | * @param listener The listener that will be called when an item is clicked. |
| 788 | * @param labelColumn The column name on the cursor containing the string to display |
| 789 | * in the label. |
| 790 | * |
| 791 | * @return This Builder object to allow for chaining of calls to set methods |
| 792 | */ |
| 793 | public Builder setCursor(final Cursor cursor, final OnClickListener listener, |
| 794 | String labelColumn) { |
| 795 | P.mCursor = cursor; |
| 796 | P.mLabelColumn = labelColumn; |
| 797 | P.mOnClickListener = listener; |
| 798 | return this; |
| 799 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 800 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | /** |
| 802 | * Set a list of items to be displayed in the dialog as the content, |
| 803 | * you will be notified of the selected item via the supplied listener. |
| 804 | * This should be an array type, e.g. R.array.foo. The list will have |
| 805 | * a check mark displayed to the right of the text for each checked |
| 806 | * item. Clicking on an item in the list will not dismiss the dialog. |
| 807 | * Clicking on a button will dismiss the dialog. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 808 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | * @param itemsId the resource id of an array i.e. R.array.foo |
| 810 | * @param checkedItems specifies which items are checked. It should be null in which case no |
| 811 | * items are checked. If non null it must be exactly the same length as the array of |
| 812 | * items. |
| 813 | * @param listener notified when an item on the list is clicked. The dialog will not be |
| 814 | * dismissed when an item is clicked. It will only be dismissed if clicked on a |
| 815 | * button, if no buttons are supplied it's up to the user to dismiss the dialog. |
| 816 | * |
| 817 | * @return This Builder object to allow for chaining of calls to set methods |
| 818 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 819 | public Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | final OnMultiChoiceClickListener listener) { |
| 821 | P.mItems = P.mContext.getResources().getTextArray(itemsId); |
| 822 | P.mOnCheckboxClickListener = listener; |
| 823 | P.mCheckedItems = checkedItems; |
| 824 | P.mIsMultiChoice = true; |
| 825 | return this; |
| 826 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | /** |
| 829 | * Set a list of items to be displayed in the dialog as the content, |
| 830 | * you will be notified of the selected item via the supplied listener. |
| 831 | * The list will have a check mark displayed to the right of the text |
| 832 | * for each checked item. Clicking on an item in the list will not |
| 833 | * dismiss the dialog. Clicking on a button will dismiss the dialog. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 834 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | * @param items the text of the items to be displayed in the list. |
| 836 | * @param checkedItems specifies which items are checked. It should be null in which case no |
| 837 | * items are checked. If non null it must be exactly the same length as the array of |
| 838 | * items. |
| 839 | * @param listener notified when an item on the list is clicked. The dialog will not be |
| 840 | * dismissed when an item is clicked. It will only be dismissed if clicked on a |
| 841 | * button, if no buttons are supplied it's up to the user to dismiss the dialog. |
| 842 | * |
| 843 | * @return This Builder object to allow for chaining of calls to set methods |
| 844 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 845 | public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | final OnMultiChoiceClickListener listener) { |
| 847 | P.mItems = items; |
| 848 | P.mOnCheckboxClickListener = listener; |
| 849 | P.mCheckedItems = checkedItems; |
| 850 | P.mIsMultiChoice = true; |
| 851 | return this; |
| 852 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 853 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | /** |
| 855 | * Set a list of items to be displayed in the dialog as the content, |
| 856 | * you will be notified of the selected item via the supplied listener. |
| 857 | * The list will have a check mark displayed to the right of the text |
| 858 | * for each checked item. Clicking on an item in the list will not |
| 859 | * dismiss the dialog. Clicking on a button will dismiss the dialog. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 860 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | * @param cursor the cursor used to provide the items. |
| 862 | * @param isCheckedColumn specifies the column name on the cursor to use to determine |
| 863 | * whether a checkbox is checked or not. It must return an integer value where 1 |
| 864 | * means checked and 0 means unchecked. |
| 865 | * @param labelColumn The column name on the cursor containing the string to display in the |
| 866 | * label. |
| 867 | * @param listener notified when an item on the list is clicked. The dialog will not be |
| 868 | * dismissed when an item is clicked. It will only be dismissed if clicked on a |
| 869 | * button, if no buttons are supplied it's up to the user to dismiss the dialog. |
| 870 | * |
| 871 | * @return This Builder object to allow for chaining of calls to set methods |
| 872 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 873 | public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | final OnMultiChoiceClickListener listener) { |
| 875 | P.mCursor = cursor; |
| 876 | P.mOnCheckboxClickListener = listener; |
| 877 | P.mIsCheckedColumn = isCheckedColumn; |
| 878 | P.mLabelColumn = labelColumn; |
| 879 | P.mIsMultiChoice = true; |
| 880 | return this; |
| 881 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 882 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | /** |
| 884 | * Set a list of items to be displayed in the dialog as the content, you will be notified of |
| 885 | * the selected item via the supplied listener. This should be an array type i.e. |
| 886 | * R.array.foo The list will have a check mark displayed to the right of the text for the |
| 887 | * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a |
| 888 | * button will dismiss the dialog. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 889 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | * @param itemsId the resource id of an array i.e. R.array.foo |
| 891 | * @param checkedItem specifies which item is checked. If -1 no items are checked. |
| 892 | * @param listener notified when an item on the list is clicked. The dialog will not be |
| 893 | * dismissed when an item is clicked. It will only be dismissed if clicked on a |
| 894 | * button, if no buttons are supplied it's up to the user to dismiss the dialog. |
| 895 | * |
| 896 | * @return This Builder object to allow for chaining of calls to set methods |
| 897 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 898 | public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | final OnClickListener listener) { |
| 900 | P.mItems = P.mContext.getResources().getTextArray(itemsId); |
| 901 | P.mOnClickListener = listener; |
| 902 | P.mCheckedItem = checkedItem; |
| 903 | P.mIsSingleChoice = true; |
| 904 | return this; |
| 905 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 906 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | /** |
| 908 | * Set a list of items to be displayed in the dialog as the content, you will be notified of |
| 909 | * the selected item via the supplied listener. The list will have a check mark displayed to |
| 910 | * the right of the text for the checked item. Clicking on an item in the list will not |
| 911 | * dismiss the dialog. Clicking on a button will dismiss the dialog. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 912 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | * @param cursor the cursor to retrieve the items from. |
| 914 | * @param checkedItem specifies which item is checked. If -1 no items are checked. |
| 915 | * @param labelColumn The column name on the cursor containing the string to display in the |
| 916 | * label. |
| 917 | * @param listener notified when an item on the list is clicked. The dialog will not be |
| 918 | * dismissed when an item is clicked. It will only be dismissed if clicked on a |
| 919 | * button, if no buttons are supplied it's up to the user to dismiss the dialog. |
| 920 | * |
| 921 | * @return This Builder object to allow for chaining of calls to set methods |
| 922 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 923 | public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | final OnClickListener listener) { |
| 925 | P.mCursor = cursor; |
| 926 | P.mOnClickListener = listener; |
| 927 | P.mCheckedItem = checkedItem; |
| 928 | P.mLabelColumn = labelColumn; |
| 929 | P.mIsSingleChoice = true; |
| 930 | return this; |
| 931 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 932 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | /** |
| 934 | * Set a list of items to be displayed in the dialog as the content, you will be notified of |
| 935 | * the selected item via the supplied listener. The list will have a check mark displayed to |
| 936 | * the right of the text for the checked item. Clicking on an item in the list will not |
| 937 | * dismiss the dialog. Clicking on a button will dismiss the dialog. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 938 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | * @param items the items to be displayed. |
| 940 | * @param checkedItem specifies which item is checked. If -1 no items are checked. |
| 941 | * @param listener notified when an item on the list is clicked. The dialog will not be |
| 942 | * dismissed when an item is clicked. It will only be dismissed if clicked on a |
| 943 | * button, if no buttons are supplied it's up to the user to dismiss the dialog. |
| 944 | * |
| 945 | * @return This Builder object to allow for chaining of calls to set methods |
| 946 | */ |
| 947 | public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) { |
| 948 | P.mItems = items; |
| 949 | P.mOnClickListener = listener; |
| 950 | P.mCheckedItem = checkedItem; |
| 951 | P.mIsSingleChoice = true; |
| 952 | return this; |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 953 | } |
| 954 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | /** |
| 956 | * Set a list of items to be displayed in the dialog as the content, you will be notified of |
| 957 | * the selected item via the supplied listener. The list will have a check mark displayed to |
| 958 | * the right of the text for the checked item. Clicking on an item in the list will not |
| 959 | * dismiss the dialog. Clicking on a button will dismiss the dialog. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 960 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | * @param adapter The {@link ListAdapter} to supply the list of items |
| 962 | * @param checkedItem specifies which item is checked. If -1 no items are checked. |
| 963 | * @param listener notified when an item on the list is clicked. The dialog will not be |
| 964 | * dismissed when an item is clicked. It will only be dismissed if clicked on a |
| 965 | * button, if no buttons are supplied it's up to the user to dismiss the dialog. |
| 966 | * |
| 967 | * @return This Builder object to allow for chaining of calls to set methods |
| 968 | */ |
| 969 | public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) { |
| 970 | P.mAdapter = adapter; |
| 971 | P.mOnClickListener = listener; |
| 972 | P.mCheckedItem = checkedItem; |
| 973 | P.mIsSingleChoice = true; |
| 974 | return this; |
| 975 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 976 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | /** |
| 978 | * 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] | 979 | * |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 980 | * @param listener the listener to be invoked |
| 981 | * @return this Builder object to allow for chaining of calls to set methods |
| 982 | * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | */ |
| 984 | public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) { |
| 985 | P.mOnItemSelectedListener = listener; |
| 986 | return this; |
| 987 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 988 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | /** |
Alan Viverette | ec18670 | 2013-12-05 11:10:31 -0800 | [diff] [blame] | 990 | * Set a custom view resource to be the contents of the Dialog. The |
| 991 | * resource will be inflated, adding all top-level views to the screen. |
| 992 | * |
| 993 | * @param layoutResId Resource ID to be inflated. |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 994 | * @return this Builder object to allow for chaining of calls to set |
Alan Viverette | ec18670 | 2013-12-05 11:10:31 -0800 | [diff] [blame] | 995 | * methods |
| 996 | */ |
| 997 | public Builder setView(int layoutResId) { |
| 998 | P.mView = null; |
| 999 | P.mViewLayoutResId = layoutResId; |
| 1000 | P.mViewSpacingSpecified = false; |
| 1001 | return this; |
| 1002 | } |
| 1003 | |
| 1004 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1005 | * Sets a custom view to be the contents of the alert dialog. |
| 1006 | * <p> |
| 1007 | * When using a pre-Holo theme, if the supplied view is an instance of |
| 1008 | * a {@link ListView} then the light background will be used. |
| 1009 | * <p> |
| 1010 | * <strong>Note:</strong> To ensure consistent styling, the custom view |
| 1011 | * should be inflated or constructed using the alert dialog's themed |
| 1012 | * context obtained via {@link #getContext()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | * |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1014 | * @param view the view to use as the contents of the alert dialog |
| 1015 | * @return this Builder object to allow for chaining of calls to set |
| 1016 | * methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | */ |
| 1018 | public Builder setView(View view) { |
| 1019 | P.mView = view; |
Alan Viverette | ec18670 | 2013-12-05 11:10:31 -0800 | [diff] [blame] | 1020 | P.mViewLayoutResId = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | P.mViewSpacingSpecified = false; |
| 1022 | return this; |
| 1023 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1024 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1026 | * Sets a custom view to be the contents of the alert dialog and |
| 1027 | * specifies additional padding around that view. |
| 1028 | * <p> |
| 1029 | * When using a pre-Holo theme, if the supplied view is an instance of |
| 1030 | * a {@link ListView} then the light background will be used. |
| 1031 | * <p> |
| 1032 | * <strong>Note:</strong> To ensure consistent styling, the custom view |
| 1033 | * should be inflated or constructed using the alert dialog's themed |
| 1034 | * context obtained via {@link #getContext()}. |
| 1035 | * |
| 1036 | * @param view the view to use as the contents of the alert dialog |
| 1037 | * @param viewSpacingLeft spacing between the left edge of the view and |
| 1038 | * the dialog frame |
| 1039 | * @param viewSpacingTop spacing between the top edge of the view and |
| 1040 | * the dialog frame |
| 1041 | * @param viewSpacingRight spacing between the right edge of the view |
| 1042 | * and the dialog frame |
| 1043 | * @param viewSpacingBottom spacing between the bottom edge of the view |
| 1044 | * and the dialog frame |
| 1045 | * @return this Builder object to allow for chaining of calls to set |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | * methods |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1047 | * |
| 1048 | * @hide Remove once the framework usages have been replaced. |
| 1049 | * @deprecated Set the padding on the view itself. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1051 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop, |
| 1053 | int viewSpacingRight, int viewSpacingBottom) { |
| 1054 | P.mView = view; |
Alan Viverette | ec18670 | 2013-12-05 11:10:31 -0800 | [diff] [blame] | 1055 | P.mViewLayoutResId = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | P.mViewSpacingSpecified = true; |
| 1057 | P.mViewSpacingLeft = viewSpacingLeft; |
| 1058 | P.mViewSpacingTop = viewSpacingTop; |
| 1059 | P.mViewSpacingRight = viewSpacingRight; |
| 1060 | P.mViewSpacingBottom = viewSpacingBottom; |
| 1061 | return this; |
| 1062 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1063 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1065 | * Sets the alert dialog to use the inverse background, regardless of |
| 1066 | * what the contents is. |
| 1067 | * |
| 1068 | * @param useInverseBackground whether to use the inverse background |
| 1069 | * @return this Builder object to allow for chaining of calls to set methods |
| 1070 | * @deprecated This flag is only used for pre-Material themes. Instead, |
| 1071 | * specify the window background using on the alert dialog |
| 1072 | * theme. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | */ |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1074 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | public Builder setInverseBackgroundForced(boolean useInverseBackground) { |
| 1076 | P.mForceInverseBackground = useInverseBackground; |
| 1077 | return this; |
| 1078 | } |
Romain Guy | 9c802c1 | 2009-03-25 15:07:31 -0700 | [diff] [blame] | 1079 | |
| 1080 | /** |
| 1081 | * @hide |
| 1082 | */ |
| 1083 | public Builder setRecycleOnMeasureEnabled(boolean enabled) { |
| 1084 | P.mRecycleOnMeasure = enabled; |
| 1085 | return this; |
| 1086 | } |
| 1087 | |
| 1088 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1090 | * Creates an {@link AlertDialog} with the arguments supplied to this |
| 1091 | * builder. |
| 1092 | * <p> |
| 1093 | * Calling this method does not display the dialog. If no additional |
| 1094 | * processing is needed, {@link #show()} may be called instead to both |
| 1095 | * create and display the dialog. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1096 | */ |
| 1097 | public AlertDialog create() { |
Alan Viverette | 93d9e26 | 2015-08-11 15:02:42 -0400 | [diff] [blame] | 1098 | // Context has already been wrapped with the appropriate theme. |
| 1099 | final AlertDialog dialog = new AlertDialog(P.mContext, 0, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | P.apply(dialog.mAlert); |
Adam Powell | fbca769 | 2011-06-22 21:29:19 -0700 | [diff] [blame] | 1101 | dialog.setCancelable(P.mCancelable); |
| 1102 | if (P.mCancelable) { |
| 1103 | dialog.setCanceledOnTouchOutside(true); |
| 1104 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | dialog.setOnCancelListener(P.mOnCancelListener); |
Adam Powell | 7f02dc5 | 2012-08-27 13:35:16 -0700 | [diff] [blame] | 1106 | dialog.setOnDismissListener(P.mOnDismissListener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | if (P.mOnKeyListener != null) { |
| 1108 | dialog.setOnKeyListener(P.mOnKeyListener); |
| 1109 | } |
| 1110 | return dialog; |
| 1111 | } |
| 1112 | |
| 1113 | /** |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1114 | * Creates an {@link AlertDialog} with the arguments supplied to this |
| 1115 | * builder and immediately displays the dialog. |
| 1116 | * <p> |
| 1117 | * Calling this method is functionally identical to: |
| 1118 | * <pre> |
| 1119 | * AlertDialog dialog = builder.create(); |
| 1120 | * dialog.show(); |
| 1121 | * </pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | */ |
| 1123 | public AlertDialog show() { |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1124 | final AlertDialog dialog = create(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | dialog.show(); |
| 1126 | return dialog; |
| 1127 | } |
| 1128 | } |
Alan Viverette | 49f118e | 2015-02-23 17:15:27 -0800 | [diff] [blame] | 1129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | } |