AlertDialog.java revision 3a2c6bf0fcae9421cccf113ff972df7aaeb6d3e9
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.content.Context; 20import android.content.DialogInterface; 21import android.database.Cursor; 22import android.graphics.drawable.Drawable; 23import android.os.Bundle; 24import android.os.Message; 25import android.view.KeyEvent; 26import android.view.View; 27import android.view.WindowManager; 28import android.widget.AdapterView; 29import android.widget.Button; 30import android.widget.ListAdapter; 31import android.widget.ListView; 32 33import com.android.internal.app.AlertController; 34 35/** 36 * A subclass of Dialog that can display one, two or three buttons. If you only want to 37 * display a String in this dialog box, use the setMessage() method. If you 38 * want to display a more complex view, look up the FrameLayout called "custom" 39 * and add your view to it: 40 * 41 * <pre> 42 * FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom); 43 * fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); 44 * </pre> 45 * 46 * <p>The AlertDialog class takes care of automatically setting 47 * {@link WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM 48 * WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} for you based on whether 49 * any views in the dialog return true from {@link View#onCheckIsTextEditor() 50 * View.onCheckIsTextEditor()}. Generally you want this set for a Dialog 51 * without text editors, so that it will be placed on top of the current 52 * input method UI. You can modify this behavior by forcing the flag to your 53 * desired mode after calling {@link #onCreate}. 54 */ 55public class AlertDialog extends Dialog implements DialogInterface { 56 private AlertController mAlert; 57 58 protected AlertDialog(Context context) { 59 this(context, com.android.internal.R.style.Theme_Dialog_Alert); 60 } 61 62 protected AlertDialog(Context context, int theme) { 63 super(context, theme); 64 mAlert = new AlertController(context, this, getWindow()); 65 } 66 67 protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { 68 super(context, com.android.internal.R.style.Theme_Dialog_Alert); 69 setCancelable(cancelable); 70 setOnCancelListener(cancelListener); 71 mAlert = new AlertController(context, this, getWindow()); 72 } 73 74 /** 75 * Gets one of the buttons used in the dialog. 76 * <p> 77 * If a button does not exist in the dialog, null will be returned. 78 * 79 * @param whichButton The identifier of the button that should be returned. 80 * For example, this can be 81 * {@link DialogInterface#BUTTON_POSITIVE}. 82 * @return The button from the dialog, or null if a button does not exist. 83 */ 84 public Button getButton(int whichButton) { 85 return mAlert.getButton(whichButton); 86 } 87 88 /** 89 * Gets the list view used in the dialog. 90 * 91 * @return The {@link ListView} from the dialog. 92 */ 93 public ListView getListView() { 94 return mAlert.getListView(); 95 } 96 97 @Override 98 public void setTitle(CharSequence title) { 99 super.setTitle(title); 100 mAlert.setTitle(title); 101 } 102 103 /** 104 * @see Builder#setCustomTitle(View) 105 */ 106 public void setCustomTitle(View customTitleView) { 107 mAlert.setCustomTitle(customTitleView); 108 } 109 110 public void setMessage(CharSequence message) { 111 mAlert.setMessage(message); 112 } 113 114 /** 115 * Set the view to display in that dialog. 116 */ 117 public void setView(View view) { 118 mAlert.setView(view); 119 } 120 121 /** 122 * Set the view to display in that dialog, specifying the spacing to appear around that 123 * view. 124 * 125 * @param view The view to show in the content area of the dialog 126 * @param viewSpacingLeft Extra space to appear to the left of {@code view} 127 * @param viewSpacingTop Extra space to appear above {@code view} 128 * @param viewSpacingRight Extra space to appear to the right of {@code view} 129 * @param viewSpacingBottom Extra space to appear below {@code view} 130 */ 131 public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, 132 int viewSpacingBottom) { 133 mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom); 134 } 135 136 /** 137 * Set a message to be sent when a button is pressed. 138 * 139 * @param whichButton Which button to set the message for, can be one of 140 * {@link DialogInterface#BUTTON_POSITIVE}, 141 * {@link DialogInterface#BUTTON_NEGATIVE}, or 142 * {@link DialogInterface#BUTTON_NEUTRAL} 143 * @param text The text to display in positive button. 144 * @param msg The {@link Message} to be sent when clicked. 145 */ 146 public void setButton(int whichButton, CharSequence text, Message msg) { 147 mAlert.setButton(whichButton, text, null, msg); 148 } 149 150 /** 151 * Set a listener to be invoked when the positive button of the dialog is pressed. 152 * 153 * @param whichButton Which button to set the listener on, can be one of 154 * {@link DialogInterface#BUTTON_POSITIVE}, 155 * {@link DialogInterface#BUTTON_NEGATIVE}, or 156 * {@link DialogInterface#BUTTON_NEUTRAL} 157 * @param text The text to display in positive button. 158 * @param listener The {@link DialogInterface.OnClickListener} to use. 159 */ 160 public void setButton(int whichButton, CharSequence text, OnClickListener listener) { 161 mAlert.setButton(whichButton, text, listener, null); 162 } 163 164 /** 165 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with 166 * {@link DialogInterface#BUTTON_POSITIVE}. 167 */ 168 @Deprecated 169 public void setButton(CharSequence text, Message msg) { 170 setButton(BUTTON_POSITIVE, text, msg); 171 } 172 173 /** 174 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with 175 * {@link DialogInterface#BUTTON_NEGATIVE}. 176 */ 177 @Deprecated 178 public void setButton2(CharSequence text, Message msg) { 179 setButton(BUTTON_NEGATIVE, text, msg); 180 } 181 182 /** 183 * @deprecated Use {@link #setButton(int, CharSequence, Message)} with 184 * {@link DialogInterface#BUTTON_NEUTRAL}. 185 */ 186 @Deprecated 187 public void setButton3(CharSequence text, Message msg) { 188 setButton(BUTTON_NEUTRAL, text, msg); 189 } 190 191 /** 192 * Set a listener to be invoked when button 1 of the dialog is pressed. 193 * 194 * @param text The text to display in button 1. 195 * @param listener The {@link DialogInterface.OnClickListener} to use. 196 * @deprecated Use 197 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)} 198 * with {@link DialogInterface#BUTTON_POSITIVE} 199 */ 200 @Deprecated 201 public void setButton(CharSequence text, final OnClickListener listener) { 202 setButton(BUTTON_POSITIVE, text, listener); 203 } 204 205 /** 206 * Set a listener to be invoked when button 2 of the dialog is pressed. 207 * @param text The text to display in button 2. 208 * @param listener The {@link DialogInterface.OnClickListener} to use. 209 * @deprecated Use 210 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)} 211 * with {@link DialogInterface#BUTTON_NEGATIVE} 212 */ 213 @Deprecated 214 public void setButton2(CharSequence text, final OnClickListener listener) { 215 setButton(BUTTON_NEGATIVE, text, listener); 216 } 217 218 /** 219 * Set a listener to be invoked when button 3 of the dialog is pressed. 220 * @param text The text to display in button 3. 221 * @param listener The {@link DialogInterface.OnClickListener} to use. 222 * @deprecated Use 223 * {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)} 224 * with {@link DialogInterface#BUTTON_POSITIVE} 225 */ 226 @Deprecated 227 public void setButton3(CharSequence text, final OnClickListener listener) { 228 setButton(BUTTON_NEUTRAL, text, listener); 229 } 230 231 /** 232 * Set resId to 0 if you don't want an icon. 233 * @param resId the resourceId of the drawable to use as the icon or 0 234 * if you don't want an icon. 235 */ 236 public void setIcon(int resId) { 237 mAlert.setIcon(resId); 238 } 239 240 public void setIcon(Drawable icon) { 241 mAlert.setIcon(icon); 242 } 243 244 public void setInverseBackgroundForced(boolean forceInverseBackground) { 245 mAlert.setInverseBackgroundForced(forceInverseBackground); 246 } 247 248 @Override 249 protected void onCreate(Bundle savedInstanceState) { 250 super.onCreate(savedInstanceState); 251 mAlert.installContent(); 252 } 253 254 @Override 255 public boolean onKeyDown(int keyCode, KeyEvent event) { 256 if (mAlert.onKeyDown(keyCode, event)) return true; 257 return super.onKeyDown(keyCode, event); 258 } 259 260 @Override 261 public boolean onKeyUp(int keyCode, KeyEvent event) { 262 if (mAlert.onKeyUp(keyCode, event)) return true; 263 return super.onKeyUp(keyCode, event); 264 } 265 266 public static class Builder { 267 private final AlertController.AlertParams P; 268 private int mTheme; 269 270 /** 271 * Constructor using a context for this builder and the {@link AlertDialog} it creates. 272 */ 273 public Builder(Context context) { 274 this(context, com.android.internal.R.style.Theme_Dialog_Alert); 275 } 276 277 /** 278 * Constructor using a context and theme for this builder and 279 * the {@link AlertDialog} it creates. 280 */ 281 public Builder(Context context, int theme) { 282 P = new AlertController.AlertParams(context); 283 mTheme = theme; 284 } 285 286 /** 287 * Set the title using the given resource id. 288 * 289 * @return This Builder object to allow for chaining of calls to set methods 290 */ 291 public Builder setTitle(int titleId) { 292 P.mTitle = P.mContext.getText(titleId); 293 return this; 294 } 295 296 /** 297 * Set the title displayed in the {@link Dialog}. 298 * 299 * @return This Builder object to allow for chaining of calls to set methods 300 */ 301 public Builder setTitle(CharSequence title) { 302 P.mTitle = title; 303 return this; 304 } 305 306 /** 307 * Set the title using the custom view {@code customTitleView}. The 308 * methods {@link #setTitle(int)} and {@link #setIcon(int)} should be 309 * sufficient for most titles, but this is provided if the title needs 310 * more customization. Using this will replace the title and icon set 311 * via the other methods. 312 * 313 * @param customTitleView The custom view to use as the title. 314 * 315 * @return This Builder object to allow for chaining of calls to set methods 316 */ 317 public Builder setCustomTitle(View customTitleView) { 318 P.mCustomTitleView = customTitleView; 319 return this; 320 } 321 322 /** 323 * Set the message to display using the given resource id. 324 * 325 * @return This Builder object to allow for chaining of calls to set methods 326 */ 327 public Builder setMessage(int messageId) { 328 P.mMessage = P.mContext.getText(messageId); 329 return this; 330 } 331 332 /** 333 * Set the message to display. 334 * 335 * @return This Builder object to allow for chaining of calls to set methods 336 */ 337 public Builder setMessage(CharSequence message) { 338 P.mMessage = message; 339 return this; 340 } 341 342 /** 343 * Set the resource id of the {@link Drawable} to be used in the title. 344 * 345 * @return This Builder object to allow for chaining of calls to set methods 346 */ 347 public Builder setIcon(int iconId) { 348 P.mIconId = iconId; 349 return this; 350 } 351 352 /** 353 * Set the {@link Drawable} to be used in the title. 354 * 355 * @return This Builder object to allow for chaining of calls to set methods 356 */ 357 public Builder setIcon(Drawable icon) { 358 P.mIcon = icon; 359 return this; 360 } 361 362 /** 363 * Set a listener to be invoked when the positive button of the dialog is pressed. 364 * @param textId The resource id of the text to display in the positive button 365 * @param listener The {@link DialogInterface.OnClickListener} to use. 366 * 367 * @return This Builder object to allow for chaining of calls to set methods 368 */ 369 public Builder setPositiveButton(int textId, final OnClickListener listener) { 370 P.mPositiveButtonText = P.mContext.getText(textId); 371 P.mPositiveButtonListener = listener; 372 return this; 373 } 374 375 /** 376 * Set a listener to be invoked when the positive button of the dialog is pressed. 377 * @param text The text to display in the positive button 378 * @param listener The {@link DialogInterface.OnClickListener} to use. 379 * 380 * @return This Builder object to allow for chaining of calls to set methods 381 */ 382 public Builder setPositiveButton(CharSequence text, final OnClickListener listener) { 383 P.mPositiveButtonText = text; 384 P.mPositiveButtonListener = listener; 385 return this; 386 } 387 388 /** 389 * Set a listener to be invoked when the negative button of the dialog is pressed. 390 * @param textId The resource id of the text to display in the negative button 391 * @param listener The {@link DialogInterface.OnClickListener} to use. 392 * 393 * @return This Builder object to allow for chaining of calls to set methods 394 */ 395 public Builder setNegativeButton(int textId, final OnClickListener listener) { 396 P.mNegativeButtonText = P.mContext.getText(textId); 397 P.mNegativeButtonListener = listener; 398 return this; 399 } 400 401 /** 402 * Set a listener to be invoked when the negative button of the dialog is pressed. 403 * @param text The text to display in the negative button 404 * @param listener The {@link DialogInterface.OnClickListener} to use. 405 * 406 * @return This Builder object to allow for chaining of calls to set methods 407 */ 408 public Builder setNegativeButton(CharSequence text, final OnClickListener listener) { 409 P.mNegativeButtonText = text; 410 P.mNegativeButtonListener = listener; 411 return this; 412 } 413 414 /** 415 * Set a listener to be invoked when the neutral button of the dialog is pressed. 416 * @param textId The resource id of the text to display in the neutral button 417 * @param listener The {@link DialogInterface.OnClickListener} to use. 418 * 419 * @return This Builder object to allow for chaining of calls to set methods 420 */ 421 public Builder setNeutralButton(int textId, final OnClickListener listener) { 422 P.mNeutralButtonText = P.mContext.getText(textId); 423 P.mNeutralButtonListener = listener; 424 return this; 425 } 426 427 /** 428 * Set a listener to be invoked when the neutral button of the dialog is pressed. 429 * @param text The text to display in the neutral button 430 * @param listener The {@link DialogInterface.OnClickListener} to use. 431 * 432 * @return This Builder object to allow for chaining of calls to set methods 433 */ 434 public Builder setNeutralButton(CharSequence text, final OnClickListener listener) { 435 P.mNeutralButtonText = text; 436 P.mNeutralButtonListener = listener; 437 return this; 438 } 439 440 /** 441 * Sets whether the dialog is cancelable or not. Default is true. 442 * 443 * @return This Builder object to allow for chaining of calls to set methods 444 */ 445 public Builder setCancelable(boolean cancelable) { 446 P.mCancelable = cancelable; 447 return this; 448 } 449 450 /** 451 * Sets the callback that will be called if the dialog is canceled. 452 * @see #setCancelable(boolean) 453 * 454 * @return This Builder object to allow for chaining of calls to set methods 455 */ 456 public Builder setOnCancelListener(OnCancelListener onCancelListener) { 457 P.mOnCancelListener = onCancelListener; 458 return this; 459 } 460 461 /** 462 * Sets the callback that will be called if a key is dispatched to the dialog. 463 * 464 * @return This Builder object to allow for chaining of calls to set methods 465 */ 466 public Builder setOnKeyListener(OnKeyListener onKeyListener) { 467 P.mOnKeyListener = onKeyListener; 468 return this; 469 } 470 471 /** 472 * Set a list of items to be displayed in the dialog as the content, you will be notified of the 473 * selected item via the supplied listener. This should be an array type i.e. R.array.foo 474 * 475 * @return This Builder object to allow for chaining of calls to set methods 476 */ 477 public Builder setItems(int itemsId, final OnClickListener listener) { 478 P.mItems = P.mContext.getResources().getTextArray(itemsId); 479 P.mOnClickListener = listener; 480 return this; 481 } 482 483 /** 484 * Set a list of items to be displayed in the dialog as the content, you will be notified of the 485 * selected item via the supplied listener. 486 * 487 * @return This Builder object to allow for chaining of calls to set methods 488 */ 489 public Builder setItems(CharSequence[] items, final OnClickListener listener) { 490 P.mItems = items; 491 P.mOnClickListener = listener; 492 return this; 493 } 494 495 /** 496 * Set a list of items, which are supplied by the given {@link ListAdapter}, to be 497 * displayed in the dialog as the content, you will be notified of the 498 * selected item via the supplied listener. 499 * 500 * @param adapter The {@link ListAdapter} to supply the list of items 501 * @param listener The listener that will be called when an item is clicked. 502 * 503 * @return This Builder object to allow for chaining of calls to set methods 504 */ 505 public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) { 506 P.mAdapter = adapter; 507 P.mOnClickListener = listener; 508 return this; 509 } 510 511 /** 512 * Set a list of items, which are supplied by the given {@link Cursor}, to be 513 * displayed in the dialog as the content, you will be notified of the 514 * selected item via the supplied listener. 515 * 516 * @param cursor The {@link Cursor} to supply the list of items 517 * @param listener The listener that will be called when an item is clicked. 518 * @param labelColumn The column name on the cursor containing the string to display 519 * in the label. 520 * 521 * @return This Builder object to allow for chaining of calls to set methods 522 */ 523 public Builder setCursor(final Cursor cursor, final OnClickListener listener, 524 String labelColumn) { 525 P.mCursor = cursor; 526 P.mLabelColumn = labelColumn; 527 P.mOnClickListener = listener; 528 return this; 529 } 530 531 /** 532 * Set a list of items to be displayed in the dialog as the content, 533 * you will be notified of the selected item via the supplied listener. 534 * This should be an array type, e.g. R.array.foo. The list will have 535 * a check mark displayed to the right of the text for each checked 536 * item. Clicking on an item in the list will not dismiss the dialog. 537 * Clicking on a button will dismiss the dialog. 538 * 539 * @param itemsId the resource id of an array i.e. R.array.foo 540 * @param checkedItems specifies which items are checked. It should be null in which case no 541 * items are checked. If non null it must be exactly the same length as the array of 542 * items. 543 * @param listener notified when an item on the list is clicked. The dialog will not be 544 * dismissed when an item is clicked. It will only be dismissed if clicked on a 545 * button, if no buttons are supplied it's up to the user to dismiss the dialog. 546 * 547 * @return This Builder object to allow for chaining of calls to set methods 548 */ 549 public Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems, 550 final OnMultiChoiceClickListener listener) { 551 P.mItems = P.mContext.getResources().getTextArray(itemsId); 552 P.mOnCheckboxClickListener = listener; 553 P.mCheckedItems = checkedItems; 554 P.mIsMultiChoice = true; 555 return this; 556 } 557 558 /** 559 * Set a list of items to be displayed in the dialog as the content, 560 * you will be notified of the selected item via the supplied listener. 561 * The list will have a check mark displayed to the right of the text 562 * for each checked item. Clicking on an item in the list will not 563 * dismiss the dialog. Clicking on a button will dismiss the dialog. 564 * 565 * @param items the text of the items to be displayed in the list. 566 * @param checkedItems specifies which items are checked. It should be null in which case no 567 * items are checked. If non null it must be exactly the same length as the array of 568 * items. 569 * @param listener notified when an item on the list is clicked. The dialog will not be 570 * dismissed when an item is clicked. It will only be dismissed if clicked on a 571 * button, if no buttons are supplied it's up to the user to dismiss the dialog. 572 * 573 * @return This Builder object to allow for chaining of calls to set methods 574 */ 575 public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, 576 final OnMultiChoiceClickListener listener) { 577 P.mItems = items; 578 P.mOnCheckboxClickListener = listener; 579 P.mCheckedItems = checkedItems; 580 P.mIsMultiChoice = true; 581 return this; 582 } 583 584 /** 585 * Set a list of items to be displayed in the dialog as the content, 586 * you will be notified of the selected item via the supplied listener. 587 * The list will have a check mark displayed to the right of the text 588 * for each checked item. Clicking on an item in the list will not 589 * dismiss the dialog. Clicking on a button will dismiss the dialog. 590 * 591 * @param cursor the cursor used to provide the items. 592 * @param isCheckedColumn specifies the column name on the cursor to use to determine 593 * whether a checkbox is checked or not. It must return an integer value where 1 594 * means checked and 0 means unchecked. 595 * @param labelColumn The column name on the cursor containing the string to display in the 596 * label. 597 * @param listener notified when an item on the list is clicked. The dialog will not be 598 * dismissed when an item is clicked. It will only be dismissed if clicked on a 599 * button, if no buttons are supplied it's up to the user to dismiss the dialog. 600 * 601 * @return This Builder object to allow for chaining of calls to set methods 602 */ 603 public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, 604 final OnMultiChoiceClickListener listener) { 605 P.mCursor = cursor; 606 P.mOnCheckboxClickListener = listener; 607 P.mIsCheckedColumn = isCheckedColumn; 608 P.mLabelColumn = labelColumn; 609 P.mIsMultiChoice = true; 610 return this; 611 } 612 613 /** 614 * Set a list of items to be displayed in the dialog as the content, you will be notified of 615 * the selected item via the supplied listener. This should be an array type i.e. 616 * R.array.foo The list will have a check mark displayed to the right of the text for the 617 * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a 618 * button will dismiss the dialog. 619 * 620 * @param itemsId the resource id of an array i.e. R.array.foo 621 * @param checkedItem specifies which item is checked. If -1 no items are checked. 622 * @param listener notified when an item on the list is clicked. The dialog will not be 623 * dismissed when an item is clicked. It will only be dismissed if clicked on a 624 * button, if no buttons are supplied it's up to the user to dismiss the dialog. 625 * 626 * @return This Builder object to allow for chaining of calls to set methods 627 */ 628 public Builder setSingleChoiceItems(int itemsId, int checkedItem, 629 final OnClickListener listener) { 630 P.mItems = P.mContext.getResources().getTextArray(itemsId); 631 P.mOnClickListener = listener; 632 P.mCheckedItem = checkedItem; 633 P.mIsSingleChoice = true; 634 return this; 635 } 636 637 /** 638 * Set a list of items to be displayed in the dialog as the content, you will be notified of 639 * the selected item via the supplied listener. The list will have a check mark displayed to 640 * the right of the text for the checked item. Clicking on an item in the list will not 641 * dismiss the dialog. Clicking on a button will dismiss the dialog. 642 * 643 * @param cursor the cursor to retrieve the items from. 644 * @param checkedItem specifies which item is checked. If -1 no items are checked. 645 * @param labelColumn The column name on the cursor containing the string to display in the 646 * label. 647 * @param listener notified when an item on the list is clicked. The dialog will not be 648 * dismissed when an item is clicked. It will only be dismissed if clicked on a 649 * button, if no buttons are supplied it's up to the user to dismiss the dialog. 650 * 651 * @return This Builder object to allow for chaining of calls to set methods 652 */ 653 public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, 654 final OnClickListener listener) { 655 P.mCursor = cursor; 656 P.mOnClickListener = listener; 657 P.mCheckedItem = checkedItem; 658 P.mLabelColumn = labelColumn; 659 P.mIsSingleChoice = true; 660 return this; 661 } 662 663 /** 664 * Set a list of items to be displayed in the dialog as the content, you will be notified of 665 * the selected item via the supplied listener. The list will have a check mark displayed to 666 * the right of the text for the checked item. Clicking on an item in the list will not 667 * dismiss the dialog. Clicking on a button will dismiss the dialog. 668 * 669 * @param items the items to be displayed. 670 * @param checkedItem specifies which item is checked. If -1 no items are checked. 671 * @param listener notified when an item on the list is clicked. The dialog will not be 672 * dismissed when an item is clicked. It will only be dismissed if clicked on a 673 * button, if no buttons are supplied it's up to the user to dismiss the dialog. 674 * 675 * @return This Builder object to allow for chaining of calls to set methods 676 */ 677 public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) { 678 P.mItems = items; 679 P.mOnClickListener = listener; 680 P.mCheckedItem = checkedItem; 681 P.mIsSingleChoice = true; 682 return this; 683 } 684 685 /** 686 * Set a list of items to be displayed in the dialog as the content, you will be notified of 687 * the selected item via the supplied listener. The list will have a check mark displayed to 688 * the right of the text for the checked item. Clicking on an item in the list will not 689 * dismiss the dialog. Clicking on a button will dismiss the dialog. 690 * 691 * @param adapter The {@link ListAdapter} to supply the list of items 692 * @param checkedItem specifies which item is checked. If -1 no items are checked. 693 * @param listener notified when an item on the list is clicked. The dialog will not be 694 * dismissed when an item is clicked. It will only be dismissed if clicked on a 695 * button, if no buttons are supplied it's up to the user to dismiss the dialog. 696 * 697 * @return This Builder object to allow for chaining of calls to set methods 698 */ 699 public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) { 700 P.mAdapter = adapter; 701 P.mOnClickListener = listener; 702 P.mCheckedItem = checkedItem; 703 P.mIsSingleChoice = true; 704 return this; 705 } 706 707 /** 708 * Sets a listener to be invoked when an item in the list is selected. 709 * 710 * @param listener The listener to be invoked. 711 * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) 712 * 713 * @return This Builder object to allow for chaining of calls to set methods 714 */ 715 public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) { 716 P.mOnItemSelectedListener = listener; 717 return this; 718 } 719 720 /** 721 * Set a custom view to be the contents of the Dialog. If the supplied view is an instance 722 * of a {@link ListView} the light background will be used. 723 * 724 * @param view The view to use as the contents of the Dialog. 725 * 726 * @return This Builder object to allow for chaining of calls to set methods 727 */ 728 public Builder setView(View view) { 729 P.mView = view; 730 P.mViewSpacingSpecified = false; 731 return this; 732 } 733 734 /** 735 * Set a custom view to be the contents of the Dialog, specifying the 736 * spacing to appear around that view. If the supplied view is an 737 * instance of a {@link ListView} the light background will be used. 738 * 739 * @param view The view to use as the contents of the Dialog. 740 * @param viewSpacingLeft Spacing between the left edge of the view and 741 * the dialog frame 742 * @param viewSpacingTop Spacing between the top edge of the view and 743 * the dialog frame 744 * @param viewSpacingRight Spacing between the right edge of the view 745 * and the dialog frame 746 * @param viewSpacingBottom Spacing between the bottom edge of the view 747 * and the dialog frame 748 * @return This Builder object to allow for chaining of calls to set 749 * methods 750 * 751 * 752 * This is currently hidden because it seems like people should just 753 * be able to put padding around the view. 754 * @hide 755 */ 756 public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop, 757 int viewSpacingRight, int viewSpacingBottom) { 758 P.mView = view; 759 P.mViewSpacingSpecified = true; 760 P.mViewSpacingLeft = viewSpacingLeft; 761 P.mViewSpacingTop = viewSpacingTop; 762 P.mViewSpacingRight = viewSpacingRight; 763 P.mViewSpacingBottom = viewSpacingBottom; 764 return this; 765 } 766 767 /** 768 * Sets the Dialog to use the inverse background, regardless of what the 769 * contents is. 770 * 771 * @param useInverseBackground Whether to use the inverse background 772 * 773 * @return This Builder object to allow for chaining of calls to set methods 774 */ 775 public Builder setInverseBackgroundForced(boolean useInverseBackground) { 776 P.mForceInverseBackground = useInverseBackground; 777 return this; 778 } 779 780 /** 781 * @hide 782 */ 783 public Builder setRecycleOnMeasureEnabled(boolean enabled) { 784 P.mRecycleOnMeasure = enabled; 785 return this; 786 } 787 788 789 /** 790 * Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not 791 * {@link Dialog#show()} the dialog. This allows the user to do any extra processing 792 * before displaying the dialog. Use {@link #show()} if you don't have any other processing 793 * to do and want this to be created and displayed. 794 */ 795 public AlertDialog create() { 796 final AlertDialog dialog = new AlertDialog(P.mContext, mTheme); 797 P.apply(dialog.mAlert); 798 dialog.setCancelable(P.mCancelable); 799 dialog.setOnCancelListener(P.mOnCancelListener); 800 if (P.mOnKeyListener != null) { 801 dialog.setOnKeyListener(P.mOnKeyListener); 802 } 803 return dialog; 804 } 805 806 /** 807 * Creates a {@link AlertDialog} with the arguments supplied to this builder and 808 * {@link Dialog#show()}'s the dialog. 809 */ 810 public AlertDialog show() { 811 AlertDialog dialog = create(); 812 dialog.show(); 813 return dialog; 814 } 815 } 816 817} 818