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 17package android.app; 18 19import android.annotation.ArrayRes; 20import android.annotation.AttrRes; 21import android.annotation.DrawableRes; 22import android.annotation.StringRes; 23import android.annotation.StyleRes; 24import android.content.Context; 25import android.content.DialogInterface; 26import android.content.res.ResourceId; 27import android.database.Cursor; 28import android.graphics.drawable.Drawable; 29import android.os.Bundle; 30import android.os.Message; 31import android.text.Layout; 32import android.text.method.MovementMethod; 33import android.util.TypedValue; 34import android.view.ContextThemeWrapper; 35import android.view.KeyEvent; 36import android.view.View; 37import android.widget.AdapterView; 38import android.widget.Button; 39import android.widget.ListAdapter; 40import android.widget.ListView; 41 42import com.android.internal.R; 43import com.android.internal.app.AlertController; 44 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 48 * want to display a more complex view, look up the FrameLayout called "custom" 49 * and add your view to it: 50 * 51 * <pre> 52 * FrameLayout fl = findViewById(android.R.id.custom); 53 * fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); 54 * </pre> 55 * 56 * <p>The AlertDialog class takes care of automatically setting 57 * {@link android.view.WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM 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}. 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> 70 */ 71public class AlertDialog extends Dialog implements DialogInterface { 72 private AlertController mAlert; 73 74 /** 75 * Special theme constant for {@link #AlertDialog(Context, int)}: use 76 * the traditional (pre-Holo) alert dialog theme. 77 * 78 * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}. 79 */ 80 @Deprecated 81 public static final int THEME_TRADITIONAL = 1; 82 83 /** 84 * Special theme constant for {@link #AlertDialog(Context, int)}: use 85 * the holographic alert theme with a dark background. 86 * 87 * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}. 88 */ 89 @Deprecated 90 public static final int THEME_HOLO_DARK = 2; 91 92 /** 93 * Special theme constant for {@link #AlertDialog(Context, int)}: use 94 * the holographic alert theme with a light background. 95 * 96 * @deprecated Use {@link android.R.style#Theme_Material_Light_Dialog_Alert}. 97 */ 98 @Deprecated 99 public static final int THEME_HOLO_LIGHT = 3; 100 101 /** 102 * Special theme constant for {@link #AlertDialog(Context, int)}: use 103 * the device's default alert theme with a dark background. 104 * 105 * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Dialog_Alert}. 106 */ 107 @Deprecated 108 public static final int THEME_DEVICE_DEFAULT_DARK = 4; 109 110 /** 111 * Special theme constant for {@link #AlertDialog(Context, int)}: use 112 * the device's default alert theme with a light background. 113 * 114 * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Light_Dialog_Alert}. 115 */ 116 @Deprecated 117 public static final int THEME_DEVICE_DEFAULT_LIGHT = 5; 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; 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 139 * @see android.R.styleable#Theme_alertDialogTheme 140 */ 141 protected AlertDialog(Context context) { 142 this(context, 0); 143 } 144 145 /** 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 161 * @see android.R.styleable#Theme_alertDialogTheme 162 */ 163 protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { 164 this(context, 0); 165 166 setCancelable(cancelable); 167 setOnCancelListener(cancelListener); 168 } 169 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 194 * @see android.R.styleable#Theme_alertDialogTheme 195 */ 196 protected AlertDialog(Context context, @StyleRes int themeResId) { 197 this(context, themeResId, true); 198 } 199 200 AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) { 201 super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0, 202 createContextThemeWrapper); 203 204 mWindow.alwaysReadCloseOnTouchAttr(); 205 mAlert = AlertController.create(getContext(), this, getWindow()); 206 } 207 208 static @StyleRes int resolveDialogTheme(Context context, @StyleRes int themeResId) { 209 if (themeResId == THEME_TRADITIONAL) { 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; 219 } else if (ResourceId.isValid(themeResId)) { 220 // start of real resource IDs. 221 return themeResId; 222 } else { 223 final TypedValue outValue = new TypedValue(); 224 context.getTheme().resolveAttribute(R.attr.alertDialogTheme, outValue, true); 225 return outValue.resourceId; 226 } 227 } 228 229 /** 230 * Gets one of the buttons used in the dialog. Returns null if the specified 231 * button does not exist or the dialog has not yet been fully created (for 232 * example, via {@link #show()} or {@link #create()}). 233 * 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 } 242 243 /** 244 * Gets the list view used in the dialog. 245 * 246 * @return The {@link ListView} from the dialog. 247 */ 248 public ListView getListView() { 249 return mAlert.getListView(); 250 } 251 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 } 264 265 public void setMessage(CharSequence message) { 266 mAlert.setMessage(message); 267 } 268 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 280 /** 281 * Set the view to display in that dialog. 282 */ 283 public void setView(View view) { 284 mAlert.setView(view); 285 } 286 287 /** 288 * Set the view to display in that dialog, specifying the spacing to appear around that 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 /** 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 /** 311 * Set a message to be sent when a button is pressed. 312 * 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 } 323 324 /** 325 * Set a listener to be invoked when the positive button of the dialog is pressed. 326 * 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 } 346 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. 367 * 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 */ 410 public void setIcon(@DrawableRes int resId) { 411 mAlert.setIcon(resId); 412 } 413 414 public void setIcon(Drawable icon) { 415 mAlert.setIcon(icon); 416 } 417 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 */ 423 public void setIconAttribute(@AttrRes int attrId) { 424 TypedValue out = new TypedValue(); 425 mContext.getTheme().resolveAttribute(attrId, out, true); 426 mAlert.setIcon(out.resourceId); 427 } 428 429 public void setInverseBackgroundForced(boolean forceInverseBackground) { 430 mAlert.setInverseBackgroundForced(forceInverseBackground); 431 } 432 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 } 450 451 public static class Builder { 452 private final AlertController.AlertParams P; 453 454 /** 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 463 */ 464 public Builder(Context context) { 465 this(context, resolveDialogTheme(context, ResourceId.ID_NULL)); 466 } 467 468 /** 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 493 */ 494 public Builder(Context context, int themeResId) { 495 P = new AlertController.AlertParams(new ContextThemeWrapper( 496 context, resolveDialogTheme(context, themeResId))); 497 } 498 499 /** 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() { 508 return P.mContext; 509 } 510 511 /** 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 */ 516 public Builder setTitle(@StringRes int titleId) { 517 P.mTitle = P.mContext.getText(titleId); 518 return this; 519 } 520 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 } 530 531 /** 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()}. 542 * 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 546 */ 547 public Builder setCustomTitle(View customTitleView) { 548 P.mCustomTitleView = customTitleView; 549 return this; 550 } 551 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 */ 557 public Builder setMessage(@StringRes int messageId) { 558 P.mMessage = P.mContext.getText(messageId); 559 return this; 560 } 561 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 } 571 572 /** 573 * Set the resource id of the {@link Drawable} to be used in the title. 574 * <p> 575 * Takes precedence over values set using {@link #setIcon(Drawable)}. 576 * 577 * @return This Builder object to allow for chaining of calls to set methods 578 */ 579 public Builder setIcon(@DrawableRes int iconId) { 580 P.mIconId = iconId; 581 return this; 582 } 583 584 /** 585 * Set the {@link Drawable} to be used in the title. 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 593 */ 594 public Builder setIcon(Drawable icon) { 595 P.mIcon = icon; 596 return this; 597 } 598 599 /** 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)}. 605 * 606 * @param attrId ID of a theme attribute that points to a drawable resource. 607 */ 608 public Builder setIconAttribute(@AttrRes int attrId) { 609 TypedValue out = new TypedValue(); 610 P.mContext.getTheme().resolveAttribute(attrId, out, true); 611 P.mIconId = out.resourceId; 612 return this; 613 } 614 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 */ 622 public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) { 623 P.mPositiveButtonText = P.mContext.getText(textId); 624 P.mPositiveButtonListener = listener; 625 return this; 626 } 627 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 } 640 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 */ 648 public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) { 649 P.mNegativeButtonText = P.mContext.getText(textId); 650 P.mNegativeButtonListener = listener; 651 return this; 652 } 653 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 } 666 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 */ 674 public Builder setNeutralButton(@StringRes int textId, final OnClickListener listener) { 675 P.mNeutralButtonText = P.mContext.getText(textId); 676 P.mNeutralButtonListener = listener; 677 return this; 678 } 679 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 } 692 693 /** 694 * Sets whether the dialog is cancelable or not. Default is true. 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 } 702 703 /** 704 * Sets the callback that will be called if the dialog is canceled. 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> 711 * @see #setCancelable(boolean) 712 * @see #setOnDismissListener(android.content.DialogInterface.OnDismissListener) 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 } 720 721 /** 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 /** 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 } 740 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 */ 747 public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener) { 748 P.mItems = P.mContext.getResources().getTextArray(itemsId); 749 P.mOnClickListener = listener; 750 return this; 751 } 752 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 } 764 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. 769 * 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 } 780 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. 785 * 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 } 800 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. 808 * 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 */ 819 public Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems, 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 } 827 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. 834 * 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 */ 845 public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, 846 final OnMultiChoiceClickListener listener) { 847 P.mItems = items; 848 P.mOnCheckboxClickListener = listener; 849 P.mCheckedItems = checkedItems; 850 P.mIsMultiChoice = true; 851 return this; 852 } 853 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. 860 * 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 */ 873 public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, 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 } 882 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. 889 * 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 */ 898 public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem, 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 } 906 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. 912 * 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 */ 923 public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, 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 } 932 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. 938 * 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; 953 } 954 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. 960 * 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 } 976 977 /** 978 * Sets a listener to be invoked when an item in the list is selected. 979 * 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) 983 */ 984 public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) { 985 P.mOnItemSelectedListener = listener; 986 return this; 987 } 988 989 /** 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. 994 * @return this Builder object to allow for chaining of calls to set 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 /** 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()}. 1013 * 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 1017 */ 1018 public Builder setView(View view) { 1019 P.mView = view; 1020 P.mViewLayoutResId = 0; 1021 P.mViewSpacingSpecified = false; 1022 return this; 1023 } 1024 1025 /** 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 1046 * methods 1047 * 1048 * @hide Remove once the framework usages have been replaced. 1049 * @deprecated Set the padding on the view itself. 1050 */ 1051 @Deprecated 1052 public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop, 1053 int viewSpacingRight, int viewSpacingBottom) { 1054 P.mView = view; 1055 P.mViewLayoutResId = 0; 1056 P.mViewSpacingSpecified = true; 1057 P.mViewSpacingLeft = viewSpacingLeft; 1058 P.mViewSpacingTop = viewSpacingTop; 1059 P.mViewSpacingRight = viewSpacingRight; 1060 P.mViewSpacingBottom = viewSpacingBottom; 1061 return this; 1062 } 1063 1064 /** 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. 1073 */ 1074 @Deprecated 1075 public Builder setInverseBackgroundForced(boolean useInverseBackground) { 1076 P.mForceInverseBackground = useInverseBackground; 1077 return this; 1078 } 1079 1080 /** 1081 * @hide 1082 */ 1083 public Builder setRecycleOnMeasureEnabled(boolean enabled) { 1084 P.mRecycleOnMeasure = enabled; 1085 return this; 1086 } 1087 1088 1089 /** 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. 1096 */ 1097 public AlertDialog create() { 1098 // Context has already been wrapped with the appropriate theme. 1099 final AlertDialog dialog = new AlertDialog(P.mContext, 0, false); 1100 P.apply(dialog.mAlert); 1101 dialog.setCancelable(P.mCancelable); 1102 if (P.mCancelable) { 1103 dialog.setCanceledOnTouchOutside(true); 1104 } 1105 dialog.setOnCancelListener(P.mOnCancelListener); 1106 dialog.setOnDismissListener(P.mOnDismissListener); 1107 if (P.mOnKeyListener != null) { 1108 dialog.setOnKeyListener(P.mOnKeyListener); 1109 } 1110 return dialog; 1111 } 1112 1113 /** 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> 1122 */ 1123 public AlertDialog show() { 1124 final AlertDialog dialog = create(); 1125 dialog.show(); 1126 return dialog; 1127 } 1128 } 1129 1130} 1131