1page.title=Creating Dialogs 2parent.title=User Interface 3parent.link=index.html 4@jd:body 5 6<div id="qv-wrapper"> 7 <div id="qv"> 8 <h2>Key classes</h2> 9 <ol> 10 <li>{@link android.app.Dialog}</li> 11 </ol> 12 <h2>In this document</h2> 13 <ol> 14 <li><a href="#ShowingADialog">Showing a Dialog</a></li> 15 <li><a href="#DismissingADialog">Dismissing a Dialog</a></li> 16 <li><a href="#AlertDialog">Creating an AlertDialog</a> 17 <ol> 18 <li><a href="#AddingButtons">Adding buttons</a></li> 19 <li><a href="#AddingAList">Adding a list</a></li> 20 </ol> 21 </li> 22 <li><a href="#ProgressDialog">Creating a ProgressDialog</a> 23 <ol> 24 <li><a href="#ShowingAProgressBar">Showing a progress bar</a></li> 25 </ol> 26 </li> 27 <li><a href="#CustomDialog">Creating a Custom Dialog</a></li> 28 </ol> 29 </div> 30</div> 31 32<p>A dialog is usually a small window that appears in front of the current Activity. 33The underlying Activity loses focus and the dialog accepts all user interaction. 34Dialogs are normally used 35for notifications and short activities that directly relate to the application in progress.</p> 36 37<p>The Android API supports the following types of {@link android.app.Dialog} objects:</p> 38<dl> 39 <dt>{@link android.app.AlertDialog}</dt> 40 <dd>A dialog that can manage zero, one, two, or three buttons, and/or a list of 41 selectable items that can include checkboxes or radio buttons. The AlertDialog 42 is capable of constructing most dialog user interfaces and is the suggested dialog type. 43 See <a href="#AlertDialog">Creating an AlertDialog</a> below.</dd> 44 <dt>{@link android.app.ProgressDialog}</dt> 45 <dd>A dialog that displays a progress wheel or progress bar. Because it's an extension of 46 the AlertDialog, it also supports buttons. 47 See <a href="#ProgressDialog">Creating a ProgressDialog</a> below.</dd> 48 <dt>{@link android.app.DatePickerDialog}</dt> 49 <dd>A dialog that allows the user to select a date. See the 50 <a href="{@docRoot}guide/tutorials/views/hello-datepicker.html">Hello DatePicker</a> tutorial.</dd> 51 <dt>{@link android.app.TimePickerDialog}</dt> 52 <dd>A dialog that allows the user to select a time. See the 53 <a href="{@docRoot}guide/tutorials/views/hello-timepicker.html">Hello TimePicker</a> tutorial.</dd> 54</dl> 55 56<p>If you would like to customize your own dialog, you can extend the 57base {@link android.app.Dialog} object or any of the subclasses listed above and define a new layout. 58See the section on <a href="#CustomDialog">Creating a Custom Dialog</a> below.</p> 59 60 61<h2 id="ShowingADialog">Showing a Dialog</h2> 62 63<p>A dialog is always created and displayed as a part of an {@link android.app.Activity}. 64You should normally create dialogs from within your Activity's 65{@link android.app.Activity#onCreateDialog(int)} callback method. 66When you use this callback, the Android system automatically manages the state of 67each dialog and hooks them to the Activity, effectively making it the "owner" of each dialog. 68As such, each dialog inherits certain properties from the Activity. For example, when a dialog 69is open, the Menu key reveals the options menu defined for the Activity and the volume 70keys modify the audio stream used by the Activity.</p> 71 72<p class="note"><strong>Note:</strong> If you decide to create a dialog outside of the 73<code>onCreateDialog()</code> method, it will not be attached to an Activity. You can, however, 74attach it to an Activity with {@link android.app.Dialog#setOwnerActivity(Activity)}.</p> 75 76<p>When you want to show a dialog, call 77{@link android.app.Activity#showDialog(int)} and pass it an integer that uniquely identifies the 78dialog that you want to display.</p> 79 80<p>When a dialog is requested for the first time, Android calls 81{@link android.app.Activity#onCreateDialog(int)} from your Activity, which is 82where you should instantiate the {@link android.app.Dialog}. This callback method 83is passed the same ID that you passed to {@link android.app.Activity#showDialog(int)}. 84After you create the Dialog, return the object at the end of the method.</p> 85 86<p>Before the dialog is displayed, Android also calls the optional callback method 87{@link android.app.Activity#onPrepareDialog(int,Dialog)}. Define this method if you want to change 88any properties of the dialog each time it is opened. This method is called 89every time a dialog is opened, whereas {@link android.app.Activity#onCreateDialog(int)} is only 90called the very first time a dialog is opened. If you don't define 91{@link android.app.Activity#onPrepareDialog(int,Dialog) onPrepareDialog()}, then the dialog will 92remain the same as it was the previous time it was opened. This method is also passed the dialog's 93ID, along with the Dialog object you created in {@link android.app.Activity#onCreateDialog(int) 94onCreateDialog()}.</p> 95 96<p>The best way to define the {@link android.app.Activity#onCreateDialog(int)} and 97{@link android.app.Activity#onPrepareDialog(int,Dialog)} callback methods is with a 98<em>switch</em> statement that checks the <var>id</var> parameter that's passed into the method. 99Each <em>case</em> should check for a unique dialog ID and then create and define the respective Dialog. 100For example, imagine a game that uses two different dialogs: one to indicate that the game 101has paused and another to indicate that the game is over. First, define an integer ID for 102each dialog:</p> 103<pre> 104static final int DIALOG_PAUSED_ID = 0; 105static final int DIALOG_GAMEOVER_ID = 1; 106</pre> 107 108<p>Then, define the {@link android.app.Activity#onCreateDialog(int)} callback with a 109switch case for each ID:</p> 110<pre> 111protected Dialog onCreateDialog(int id) { 112 Dialog dialog; 113 switch(id) { 114 case DIALOG_PAUSED_ID: 115 // do the work to define the pause Dialog 116 break; 117 case DIALOG_GAMEOVER_ID: 118 // do the work to define the game over Dialog 119 break; 120 default: 121 dialog = null; 122 } 123 return dialog; 124} 125</pre> 126 127<p class="note"><strong>Note:</strong> In this example, there's no code inside 128the case statements because the procedure for defining your Dialog is outside the scope 129of this section. See the section below about <a href="#AlertDialog">Creating an AlertDialog</a>, 130offers code suitable for this example.</p> 131 132<p>When it's time to show one of the dialogs, call {@link android.app.Activity#showDialog(int)} 133with the ID of a dialog:</p> 134<pre> 135showDialog(DIALOG_PAUSED_ID); 136</pre> 137 138 139<h2 id="DismissingADialog">Dismissing a Dialog</h2> 140 141<p>When you're ready to close your dialog, you can dismiss it by calling 142{@link android.app.Dialog#dismiss()} on the Dialog object. 143If necessary, you can also call {@link android.app.Activity#dismissDialog(int)} from the 144Activity, which effectively calls {@link android.app.Dialog#dismiss()} on the 145Dialog for you.</p> 146 147<p>If you are using {@link android.app.Activity#onCreateDialog(int)} to manage the state 148of your dialogs (as discussed in the previous section), then every time your dialog is 149dismissed, the state of the Dialog 150object is retained by the Activity. If you decide that you will no longer need this object or 151it's important that the state is cleared, then you should call 152{@link android.app.Activity#removeDialog(int)}. This will remove any internal references 153to the object and if the dialog is showing, it will dismiss it.</p> 154 155<h3>Using dismiss listeners</h3> 156 157<p>If you'd like your applcation to perform some procedures the moment that a dialog is dismissed, 158then you should attach an on-dismiss listener to your Dialog.</p> 159 160<p>First define the {@link android.content.DialogInterface.OnDismissListener} interface. 161This interface has just one method, 162{@link android.content.DialogInterface.OnDismissListener#onDismiss(DialogInterface)}, which 163will be called when the dialog is dismissed. 164Then simply pass your OnDismissListener implementation to 165{@link android.app.Dialog#setOnDismissListener(DialogInterface.OnDismissListener) 166setOnDismissListener()}.</p> 167 168<p>However, note that dialogs can also be "cancelled." This is a special case that indicates 169the dialog was explicitly cancelled by the user. This will occur if the user presses the 170"back" button to close the dialog, or if the dialog explicitly calls {@link android.app.Dialog#cancel()} 171(perhaps from a "Cancel" button in the dialog). When a dialog is cancelled, 172the OnDismissListener will still be notified, but if you'd like to be informed that the dialog 173was explicitly cancelled (and not dismissed normally), then you should register 174an {@link android.content.DialogInterface.OnCancelListener} with 175{@link android.app.Dialog#setOnCancelListener(DialogInterface.OnCancelListener) 176setOnCancelListener()}.</p> 177 178 179<h2 id="AlertDialog">Creating an AlertDialog</h2> 180 181<p>An {@link android.app.AlertDialog} is an extension of the {@link android.app.Dialog} 182class. It is capable of constructing most dialog user interfaces and is the suggested dialog type. 183You should use it for dialogs that use any of the following features:</p> 184<ul> 185 <li>A title</li> 186 <li>A text message</li> 187 <li>One, two, or three buttons</li> 188 <li>A list of selectable items (with optional checkboxes or radio buttons)</li> 189</ul> 190 191<p>To create an AlertDialog, use the {@link android.app.AlertDialog.Builder} subclass. 192Get a Builder with {@link android.app.AlertDialog.Builder#AlertDialog.Builder(Context)} and 193then use the class's public methods to define all of the 194AlertDialog properties. After you're done with the Builder, retrieve the 195AlertDialog object with {@link android.app.AlertDialog.Builder#create()}.</p> 196 197<p>The following topics show how to define various properties of the AlertDialog using the 198AlertDialog.Builder class. If you use any of the following sample code inside your 199{@link android.app.Activity#onCreateDialog(int) onCreateDialog()} callback method, 200you can return the resulting Dialog object to display the dialog.</p> 201 202 203<h3 id="AddingButtons">Adding buttons</h3> 204 205<img src="{@docRoot}images/dialog_buttons.png" alt="" style="float:right" /> 206 207<p>To create an AlertDialog with side-by-side buttons like the one shown in the screenshot to the right, 208use the <code>set...Button()</code> methods:</p> 209 210<pre> 211AlertDialog.Builder builder = new AlertDialog.Builder(this); 212builder.setMessage("Are you sure you want to exit?") 213 .setCancelable(false) 214 .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 215 public void onClick(DialogInterface dialog, int id) { 216 MyActivity.this.finish(); 217 } 218 }) 219 .setNegativeButton("No", new DialogInterface.OnClickListener() { 220 public void onClick(DialogInterface dialog, int id) { 221 dialog.cancel(); 222 } 223 }); 224AlertDialog alert = builder.create(); 225</pre> 226 227<p>First, add a message for the dialog with 228{@link android.app.AlertDialog.Builder#setMessage(CharSequence)}. Then, begin 229method-chaining and set the dialog 230to be <em>not cancelable</em> (so the user cannot close the dialog with the back button) 231with {@link android.app.AlertDialog.Builder#setCancelable(boolean)}. For each button, 232use one of the <code>set...Button()</code> methods, such as 233{@link android.app.AlertDialog.Builder#setPositiveButton(CharSequence,DialogInterface.OnClickListener) 234setPositiveButton()}, that accepts the name for the button and a 235{@link android.content.DialogInterface.OnClickListener} that defines the action to take 236when the user selects the button.</p> 237 238<p class="note"><strong>Note:</strong> You can only add one of each button type to the 239AlertDialog. That is, you cannot have more than one "positive" button. This limits the number 240of possible buttons to three: positive, neutral, and negative. These names are technically irrelevant to the 241actual functionality of your buttons, but should help you keep track of which one does what.</p> 242 243 244<h3 id="AddingAList">Adding a list</h3> 245 246<img src="{@docRoot}images/dialog_list.png" alt="" style="float:right" /> 247 248<p>To create an AlertDialog with a list of selectable items like the one shown to the right, 249use the <code>setItems()</code> method:</p> 250 251<pre> 252final CharSequence[] items = {"Red", "Green", "Blue"}; 253 254AlertDialog.Builder builder = new AlertDialog.Builder(this); 255builder.setTitle("Pick a color"); 256builder.setItems(items, new DialogInterface.OnClickListener() { 257 public void onClick(DialogInterface dialog, int item) { 258 Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 259 } 260}); 261AlertDialog alert = builder.create(); 262</pre> 263 264<p>First, add a title to the dialog with 265{@link android.app.AlertDialog.Builder#setTitle(CharSequence)}. 266Then, add a list of selectable items with 267{@link android.app.AlertDialog.Builder#setItems(CharSequence[],DialogInterface.OnClickListener) 268setItems()}, which accepts the array of items to display and a 269{@link android.content.DialogInterface.OnClickListener} that defines the action to take 270when the user selects an item.</p> 271 272 273<h4>Adding checkboxes and radio buttons</h4> 274 275<img src="{@docRoot}images/dialog_singlechoicelist.png" alt="" style="float:right" /> 276 277<p>To create a list of multiple-choice items (checkboxes) or 278single-choice items (radio buttons) inside the dialog, use the 279{@link android.app.AlertDialog.Builder#setMultiChoiceItems(Cursor,String,String, 280DialogInterface.OnMultiChoiceClickListener) setMultiChoiceItems()} and 281{@link android.app.AlertDialog.Builder#setSingleChoiceItems(int,int,DialogInterface.OnClickListener) 282setSingleChoiceItems()} methods, respectively. 283If you create one of these selectable lists in the 284{@link android.app.Activity#onCreateDialog(int) onCreateDialog()} callback method, 285Android manages the state of the list for you. As long as the Activity is active, 286the dialog remembers the items that were previously selected, but when the user exits the 287Activity, the selection is lost. 288 289<p class="note"><strong>Note:</strong> To save the selection when the user leaves or 290pauses the Activity, you must properly save and restore the setting throughout 291the <a href="{@docRoot}guide/topics/fundamentals.html#lcycles">Activity Lifecycle</a>. 292To permanently save the selections, even when the Activity process is completely shutdown, 293you need to save the settings 294with one of the <a href="{@docRoot}guide/topics/data/data-storage.html">Data 295Storage</a> techniques.</p> 296 297<p>To create an AlertDialog with a list of single-choice items like the one shown to the right, 298use the same code from the previous example, but replace the <code>setItems()</code> method with 299{@link android.app.AlertDialog.Builder#setSingleChoiceItems(int,int,DialogInterface.OnClickListener) 300setSingleChoiceItems()}:</p> 301 302<pre> 303final CharSequence[] items = {"Red", "Green", "Blue"}; 304 305AlertDialog.Builder builder = new AlertDialog.Builder(this); 306builder.setTitle("Pick a color"); 307builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 308 public void onClick(DialogInterface dialog, int item) { 309 Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 310 } 311}); 312AlertDialog alert = builder.create(); 313</pre> 314 315<p>The second parameter in the 316{@link android.app.AlertDialog.Builder#setSingleChoiceItems(CharSequence[],int,DialogInterface.OnClickListener) 317setSingleChoiceItems()} method is an integer value for the <var>checkedItem</var>, which indicates the 318zero-based list position of the default selected item. Use "-1" to indicate that no item should be 319selected by default.</p> 320 321 322<h2 id="ProgressDialog">Creating a ProgressDialog</h2> 323 324<img src="{@docRoot}images/dialog_progress_spinning.png" alt="" style="float:right" /> 325 326<p>A {@link android.app.ProgressDialog} is an extension of the {@link android.app.AlertDialog} 327class that can display a progress animation in the form of a spinning wheel, for a task with 328progress that's undefined, or a progress bar, for a task that has a defined progression. 329The dialog can also provide buttons, such as one to cancel a download.</p> 330 331<p>Opening a progress dialog can be as simple as calling 332{@link android.app.ProgressDialog#show(Context,CharSequence,CharSequence) 333ProgressDialog.show()}. For example, the progress dialog shown to the right can be 334easily achieved without managing the dialog through the 335{@link android.app.Activity#onCreateDialog(int)} callback, 336as shown here:</p> 337 338<pre> 339ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 340 "Loading. Please wait...", true); 341</pre> 342 343<p>The first parameter is the application {@link android.content.Context}, 344the second is a title for the dialog (left empty), the third is the message, 345and the last parameter is whether the progress 346is indeterminate (this is only relevant when creating a progress bar, which is 347discussed in the next section). 348</p> 349 350<p>The default style of a progress dialog is the spinning wheel. 351If you want to create a progress bar that shows the loading progress with granularity, 352some more code is required, as discussed in the next section.</p> 353 354 355<h3 id="ShowingAProgressBar">Showing a progress bar</h3> 356 357<img src="/images/dialog_progress_bar.png" alt="" style="float:right" /> 358 359<p>To show the progression with an animated progress bar:</p> 360 361<ol> 362 <li>Initialize the 363 ProgressDialog with the class constructor, 364 {@link android.app.ProgressDialog#ProgressDialog(Context)}.</li> 365 <li>Set the progress style to "STYLE_HORIZONTAL" with 366 {@link android.app.ProgressDialog#setProgressStyle(int)} and 367 set any other properties, such as the message.</li> 368 <li>When you're ready to show the dialog, call 369 {@link android.app.Dialog#show()} or return the ProgressDialog from the 370 {@link android.app.Activity#onCreateDialog(int)} callback.</li> 371 <li>You can increment the amount of progress displayed 372 in the bar by calling either {@link android.app.ProgressDialog#setProgress(int)} with a value for 373 the total percentage completed so far or {@link android.app.ProgressDialog#incrementProgressBy(int)} 374 with an incremental value to add to the total percentage completed so far.</li> 375</ol> 376 377<p>For example, your setup might look like this:</p> 378<pre> 379ProgressDialog progressDialog; 380progressDialog = new ProgressDialog(mContext); 381progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 382progressDialog.setMessage("Loading..."); 383progressDialog.setCancelable(false); 384</pre> 385 386<p>The setup is simple. Most of the code needed to create a progress dialog is actually 387involved in the process that updates it. You might find that it's 388necessary to create a second thread in your application for this work and then report the progress 389back to the Activity's UI thread with a {@link android.os.Handler} object. 390If you're not familiar with using additional 391threads with a Handler, see the example Activity below that uses a second thread to 392increment a progress dialog managed by the Activity.</p> 393 394<script type="text/javascript"> 395function toggleDiv(link) { 396 var toggleable = $(link).parent(); 397 if (toggleable.hasClass("closed")) { 398 $(".toggleme", toggleable).slideDown("fast"); 399 toggleable.removeClass("closed"); 400 toggleable.addClass("open"); 401 $(".toggle-img", toggleable).attr("title", "hide").attr("src", "/assets/images/triangle-opened.png"); 402 } else { 403 $(".toggleme", toggleable).slideUp("fast"); 404 toggleable.removeClass("open"); 405 toggleable.addClass("closed"); 406 $(".toggle-img", toggleable).attr("title", "show").attr("src", "/assets/images/triangle-closed.png"); 407 } 408 return false; 409} 410</script> 411<style> 412.toggleme { 413 padding:0 0 1px 0; 414} 415.toggleable a { 416 text-decoration:none; 417} 418.toggleable.closed .toggleme { 419 display:none; 420} 421#jd-content .toggle-img { 422 margin:0; 423} 424</style> 425 426<div class="toggleable closed"> 427 <a href="#" onclick="return toggleDiv(this)"> 428 <img src="/assets/images/triangle-closed.png" class="toggle-img" /> 429 <strong>Example ProgressDialog with a second thread</strong></a> 430 <div class="toggleme"> 431 <p>This example uses a second thread to track the progress of a process (which actually just 432counts up to 100). The thread sends a {@link android.os.Message} back to the main 433Activity through a {@link android.os.Handler} each time progress is made. The main Activity then updates the 434ProgressDialog.</p> 435 436<pre> 437package com.example.progressdialog; 438 439import android.app.Activity; 440import android.app.Dialog; 441import android.app.ProgressDialog; 442import android.os.Bundle; 443import android.os.Handler; 444import android.os.Message; 445import android.view.View; 446import android.view.View.OnClickListener; 447import android.widget.Button; 448 449public class NotificationTest extends Activity { 450 static final int PROGRESS_DIALOG = 0; 451 Button button; 452 ProgressThread progressThread; 453 ProgressDialog progressDialog; 454 455 /** Called when the activity is first created. */ 456 public void onCreate(Bundle savedInstanceState) { 457 super.onCreate(savedInstanceState); 458 setContentView(R.layout.main); 459 460 // Setup the button that starts the progress dialog 461 button = (Button) findViewById(R.id.progressDialog); 462 button.setOnClickListener(new OnClickListener(){ 463 public void onClick(View v) { 464 showDialog(PROGRESS_DIALOG); 465 } 466 }); 467 } 468 469 protected Dialog onCreateDialog(int id) { 470 switch(id) { 471 case PROGRESS_DIALOG: 472 progressDialog = new ProgressDialog(NotificationTest.this); 473 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 474 progressDialog.setMessage("Loading..."); 475 progressThread = new ProgressThread(handler); 476 progressThread.start(); 477 return progressDialog; 478 default: 479 return null; 480 } 481 } 482 483 // Define the Handler that receives messages from the thread and update the progress 484 final Handler handler = new Handler() { 485 public void handleMessage(Message msg) { 486 int total = msg.getData().getInt("total"); 487 progressDialog.setProgress(total); 488 if (total >= 100){ 489 dismissDialog(PROGRESS_DIALOG); 490 progressThread.setState(ProgressThread.STATE_DONE); 491 } 492 } 493 }; 494 495 /** Nested class that performs progress calculations (counting) */ 496 private class ProgressThread extends Thread { 497 Handler mHandler; 498 final static int STATE_DONE = 0; 499 final static int STATE_RUNNING = 1; 500 int mState; 501 int total; 502 503 ProgressThread(Handler h) { 504 mHandler = h; 505 } 506 507 public void run() { 508 mState = STATE_RUNNING; 509 total = 0; 510 while (mState == STATE_RUNNING) { 511 try { 512 Thread.sleep(100); 513 } catch (InterruptedException e) { 514 Log.e("ERROR", "Thread Interrupted"); 515 } 516 Message msg = mHandler.obtainMessage(); 517 Bundle b = new Bundle(); 518 b.putInt("total", total); 519 msg.setData(b); 520 mHandler.sendMessage(msg); 521 total++; 522 } 523 } 524 525 /* sets the current state for the thread, 526 * used to stop the thread */ 527 public void setState(int state) { 528 mState = state; 529 } 530 } 531} 532</pre> 533 </div> <!-- end toggleme --> 534</div> <!-- end toggleable --> 535 536 537 538<h2 id="CustomDialog">Creating a Custom Dialog</h2> 539 540<img src="{@docRoot}images/dialog_custom.png" alt="" style="float:right" /> 541 542<p>If you want a customized design for a dialog, you can create your own layout 543for the dialog window with layout and widget elements. 544After you've defined your layout, pass the root View object or 545layout resource ID to {@link android.app.Dialog#setContentView(View)}.</p> 546 547<p>For example, to create the dialog shown to the right:</p> 548 549<ol> 550 <li>Create an XML layout saved as <code>custom_dialog.xml</code>: 551<pre> 552<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 553 android:id="@+id/layout_root" 554 android:orientation="horizontal" 555 android:layout_width="fill_parent" 556 android:layout_height="fill_parent" 557 android:padding="10dp" 558 > 559 <ImageView android:id="@+id/image" 560 android:layout_width="wrap_content" 561 android:layout_height="fill_parent" 562 android:layout_marginRight="10dp" 563 /> 564 <TextView android:id="@+id/text" 565 android:layout_width="wrap_content" 566 android:layout_height="fill_parent" 567 android:textColor="#FFF" 568 /> 569</LinearLayout> 570</pre> 571 572 <p>This XML defines an {@link android.widget.ImageView} and a {@link android.widget.TextView} 573 inside a {@link android.widget.LinearLayout}.</p> 574 <li>Set the above layout as the dialog's content view and define the content 575 for the ImageView and TextView elements:</p> 576<pre> 577Context mContext = getApplicationContext(); 578Dialog dialog = new Dialog(mContext); 579 580dialog.setContentView(R.layout.custom_dialog); 581dialog.setTitle("Custom Dialog"); 582 583TextView text = (TextView) dialog.findViewById(R.id.text); 584text.setText("Hello, this is a custom dialog!"); 585ImageView image = (ImageView) dialog.findViewById(R.id.image); 586image.setImageResource(R.drawable.android); 587</pre> 588 589 <p>After you instantiate the Dialog, set your custom layout as the dialog's content view with 590 {@link android.app.Dialog#setContentView(int)}, passing it the layout resource ID. 591 Now that the Dialog has a defined layout, you can capture View objects from the layout with 592 {@link android.app.Dialog#findViewById(int)} and modify their content.</p> 593 </li> 594 595 <li>That's it. You can now show the dialog as described in 596 <a href="#ShowingADialog">Showing A Dialog</a>.</li> 597</ol> 598 599<p>A dialog made with the base Dialog class must have a title. If you don't call 600{@link android.app.Dialog#setTitle(CharSequence) setTitle()}, then the space used for the title 601remains empty, but still visible. If you don't want 602a title at all, then you should create your custom dialog using the 603{@link android.app.AlertDialog} class. However, because an AlertDialog is created easiest with 604the {@link android.app.AlertDialog.Builder} class, you do not have access to the 605{@link android.app.Dialog#setContentView(int)} method used above. Instead, you must use 606{@link android.app.AlertDialog.Builder#setView(View)}. This method accepts a {@link android.view.View} object, 607so you need to inflate the layout's root View object from 608XML.</p> 609 610<p>To inflate the XML layout, retrieve the {@link android.view.LayoutInflater} with 611{@link android.app.Activity#getLayoutInflater()} 612(or {@link android.content.Context#getSystemService(String) getSystemService()}), 613and then call 614{@link android.view.LayoutInflater#inflate(int, ViewGroup)}, where the first parameter 615is the layout resource ID and the second is the ID of the root View. At this point, you can use 616the inflated layout to find View objects in the layout and define the content for the 617ImageView and TextView elements. Then instantiate the AlertDialog.Builder and set the 618inflated layout for the dialog with {@link android.app.AlertDialog.Builder#setView(View)}.</p> 619 620<p>Here's an example, creating a custom layout in an AlertDialog:</p> 621 622<pre> 623AlertDialog.Builder builder; 624AlertDialog alertDialog; 625 626Context mContext = getApplicationContext(); 627LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 628View layout = inflater.inflate(R.layout.custom_dialog, 629 (ViewGroup) findViewById(R.id.layout_root)); 630 631TextView text = (TextView) layout.findViewById(R.id.text); 632text.setText("Hello, this is a custom dialog!"); 633ImageView image = (ImageView) layout.findViewById(R.id.image); 634image.setImageResource(R.drawable.android); 635 636builder = new AlertDialog.Builder(mContext); 637builder.setView(layout); 638alertDialog = builder.create(); 639</pre> 640 641<p>Using an AlertDialog for your custom layout lets you 642take advantage of built-in AlertDialog features like managed buttons, 643selectable lists, a title, an icon and so on.</p> 644 645<p>For more information, refer to the reference documentation for the 646{@link android.app.Dialog} and {@link android.app.AlertDialog.Builder} 647classes.</p> 648 649 650 651