dialogs.jd revision 9316232130fe831c998c9d7be9cf67294eccdcae
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}resources/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}resources/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 application 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            return progressDialog;
476        default:
477            return null;
478        }
479    }
480
481    &#64;Override
482    protected void onPrepareDialog(int id, Dialog dialog) {
483        switch(id) {
484        case PROGRESS_DIALOG:
485            progressDialog.setProgress(0);
486            progressThread = new ProgressThread(handler);
487            progressThread.start();
488    }
489
490    // Define the Handler that receives messages from the thread and update the progress
491    final Handler handler = new Handler() {
492        public void handleMessage(Message msg) {
493            int total = msg.arg1;
494            progressDialog.setProgress(total);
495            if (total >= 100){
496                dismissDialog(PROGRESS_DIALOG);
497                progressThread.setState(ProgressThread.STATE_DONE);
498            }
499        }
500    };
501
502    /** Nested class that performs progress calculations (counting) */
503    private class ProgressThread extends Thread {
504        Handler mHandler;
505        final static int STATE_DONE = 0;
506        final static int STATE_RUNNING = 1;
507        int mState;
508        int total;
509       
510        ProgressThread(Handler h) {
511            mHandler = h;
512        }
513       
514        public void run() {
515            mState = STATE_RUNNING;   
516            total = 0;
517            while (mState == STATE_RUNNING) {
518                try {
519                    Thread.sleep(100);
520                } catch (InterruptedException e) {
521                    Log.e("ERROR", "Thread Interrupted");
522                }
523                Message msg = mHandler.obtainMessage();
524                msg.arg1 = total;
525                mHandler.sendMessage(msg);
526                total++;
527            }
528        }
529        
530        /* sets the current state for the thread,
531         * used to stop the thread */
532        public void setState(int state) {
533            mState = state;
534        }
535    }
536}
537</pre>
538  </div> <!-- end toggleme -->
539</div> <!-- end toggleable -->
540
541
542
543<h2 id="CustomDialog">Creating a Custom Dialog</h2>
544
545<img src="{@docRoot}images/dialog_custom.png" alt="" style="float:right" />
546
547<p>If you want a customized design for a dialog, you can create your own layout
548for the dialog window with layout and widget elements.
549After you've defined your layout, pass the root View object or
550layout resource ID to {@link android.app.Dialog#setContentView(View)}.</p>
551
552<p>For example, to create the dialog shown to the right:</p>
553
554<ol>
555  <li>Create an XML layout saved as <code>custom_dialog.xml</code>:
556<pre>
557&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
558              android:id="@+id/layout_root"
559              android:orientation="horizontal"
560              android:layout_width="fill_parent"
561              android:layout_height="fill_parent"
562              android:padding="10dp"
563              >
564    &lt;ImageView android:id="@+id/image"
565               android:layout_width="wrap_content"
566               android:layout_height="fill_parent"
567               android:layout_marginRight="10dp"
568               />
569    &lt;TextView android:id="@+id/text"
570              android:layout_width="wrap_content"
571              android:layout_height="fill_parent"
572              android:textColor="#FFF"
573              />
574&lt;/LinearLayout>
575</pre>
576
577    <p>This XML defines an {@link android.widget.ImageView} and a {@link android.widget.TextView}
578    inside a {@link android.widget.LinearLayout}.</p>
579  <li>Set the above layout as the dialog's content view and define the content 
580    for the ImageView and TextView elements:</p>
581<pre>
582Context mContext = getApplicationContext();
583Dialog dialog = new Dialog(mContext);
584
585dialog.setContentView(R.layout.custom_dialog);
586dialog.setTitle("Custom Dialog");
587
588TextView text = (TextView) dialog.findViewById(R.id.text);
589text.setText("Hello, this is a custom dialog!");
590ImageView image = (ImageView) dialog.findViewById(R.id.image);
591image.setImageResource(R.drawable.android);
592</pre>
593
594    <p>After you instantiate the Dialog, set your custom layout as the dialog's content view with 
595    {@link android.app.Dialog#setContentView(int)}, passing it the layout resource ID.
596    Now that the Dialog has a defined layout, you can capture View objects from the layout with
597    {@link android.app.Dialog#findViewById(int)} and modify their content.</p>
598  </li>
599
600  <li>That's it. You can now show the dialog as described in 
601    <a href="#ShowingADialog">Showing A Dialog</a>.</li>
602</ol>
603
604<p>A dialog made with the base Dialog class must have a title. If you don't call
605{@link android.app.Dialog#setTitle(CharSequence) setTitle()}, then the space used for the title
606remains empty, but still visible. If you don't want
607a title at all, then you should create your custom dialog using the
608{@link android.app.AlertDialog} class. However, because an AlertDialog is created easiest with 
609the {@link android.app.AlertDialog.Builder} class, you do not have access to the 
610{@link android.app.Dialog#setContentView(int)} method used above. Instead, you must use 
611{@link android.app.AlertDialog.Builder#setView(View)}. This method accepts a {@link android.view.View} object,
612so you need to inflate the layout's root View object from
613XML.</p>
614
615<p>To inflate the XML layout, retrieve the {@link android.view.LayoutInflater} with 
616{@link android.app.Activity#getLayoutInflater()} 
617(or {@link android.content.Context#getSystemService(String) getSystemService()}),
618and then call
619{@link android.view.LayoutInflater#inflate(int, ViewGroup)}, where the first parameter
620is the layout resource ID and the second is the ID of the root View. At this point, you can use
621the inflated layout to find View objects in the layout and define the content for the
622ImageView and TextView elements. Then instantiate the AlertDialog.Builder and set the
623inflated layout for the dialog with {@link android.app.AlertDialog.Builder#setView(View)}.</p>
624
625<p>Here's an example, creating a custom layout in an AlertDialog:</p>
626
627<pre>
628AlertDialog.Builder builder;
629AlertDialog alertDialog;
630
631Context mContext = getApplicationContext();
632LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
633View layout = inflater.inflate(R.layout.custom_dialog,
634                               (ViewGroup) findViewById(R.id.layout_root));
635
636TextView text = (TextView) layout.findViewById(R.id.text);
637text.setText("Hello, this is a custom dialog!");
638ImageView image = (ImageView) layout.findViewById(R.id.image);
639image.setImageResource(R.drawable.android);
640
641builder = new AlertDialog.Builder(mContext);
642builder.setView(layout);
643alertDialog = builder.create();
644</pre>
645
646<p>Using an AlertDialog for your custom layout lets you
647take advantage of built-in AlertDialog features like managed buttons,
648selectable lists, a title, an icon and so on.</p>
649
650<p>For more information, refer to the reference documentation for the 
651{@link android.app.Dialog} and {@link android.app.AlertDialog.Builder} 
652classes.</p>
653
654
655
656