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