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