1b10b48f62d3cac684424e4181d4e8ec61f227e95Scott Mainpage.title=Dialogs
233baa5ad7d8cdcc89ce4fbc3bc8cd537d5f5d639Joe Fernandezpage.tags=alertdialog,dialogfragment
364461bf4f261164cb9e3022761fd217fd0028ac5Scott Main
4ce878bef710c6d1e6881949c82b1293fb8435715Scott Main@jd:body
5ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
6db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
7db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
8ce878bef710c6d1e6881949c82b1293fb8435715Scott Main<div id="qv-wrapper">
9ce878bef710c6d1e6881949c82b1293fb8435715Scott Main  <div id="qv">
10ce878bef710c6d1e6881949c82b1293fb8435715Scott Main    <h2>In this document</h2>
11db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<ol>
12db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <li><a href="#DialogFragment">Creating a Dialog Fragment</a></li>
13db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <li><a href="#AlertDialog">Building an Alert Dialog</a>
14ce878bef710c6d1e6881949c82b1293fb8435715Scott Main    <ol>
15db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main      <li><a href="#AddingButtons">Adding buttons</a></li>
16db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main      <li><a href="#AddingAList">Adding a list</a></li>
17db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main      <li><a href="#CustomLayout">Creating a Custom Layout</a></li>
18ce878bef710c6d1e6881949c82b1293fb8435715Scott Main    </ol>
19db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  </li>
20db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <li><a href="#PassingEvents">Passing Events Back to the Dialog's Host</a></li>
21db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <li><a href="#ShowingADialog">Showing a Dialog</a></li>
22db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <li><a href="#FullscreenDialog">Showing a Dialog Fullscreen or as an Embedded Fragment</a>
23ec80d7f311b1a0899bb4caf5b380b07027e902d1Scott Main    <ol>
24db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main      <li><a href="#ActivityAsDialog">Showing an activity as a dialog on large screens</a></li>
2518439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    </ol>
26db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  </li>
27db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <li><a href="#DismissingADialog">Dismissing a Dialog</a></li>
28db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</ol>
2918439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main
30db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    <h2>Key classes</h2>
3118439bee6f8b525abe3f1fac69bc4cea184e1565Scott Main    <ol>
32db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main      <li>{@link android.app.DialogFragment}</li>
33db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main      <li>{@link android.app.AlertDialog}</li>
34ec80d7f311b1a0899bb4caf5b380b07027e902d1Scott Main    </ol>
35eb5dacddc443db0752529a69a9c48dab1b129a18Scott Main    
36eb5dacddc443db0752529a69a9c48dab1b129a18Scott Main    <h2>See also</h2>
37eb5dacddc443db0752529a69a9c48dab1b129a18Scott Main    <ol>
38db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main      <li><a href="{@docRoot}design/building-blocks/dialogs.html">Dialogs design guide</a></li>
39db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main      <li><a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> (Date/Time dialogs)</li>
40eb5dacddc443db0752529a69a9c48dab1b129a18Scott Main    </ol>
41ce878bef710c6d1e6881949c82b1293fb8435715Scott Main  </div>
42ce878bef710c6d1e6881949c82b1293fb8435715Scott Main</div>
43ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
44db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>A dialog is a small window that prompts the user to
45db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainmake a decision or enter additional information. A dialog does not fill the screen and is
46db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainnormally used for modal events that require users to take an action before they can proceed.</p>
47ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
4891b5ecc4869dab333ba15f378c6804eef7f4a688Scott Main<div class="note design">
49eb5dacddc443db0752529a69a9c48dab1b129a18Scott Main<p><strong>Dialog Design</strong></p>
50db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <p>For information about how to design your dialogs, including recommendations
51db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  for language, read the <a
52db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainhref="{@docRoot}design/building-blocks/dialogs.html">Dialogs</a> design guide.</p>
53eb5dacddc443db0752529a69a9c48dab1b129a18Scott Main</div>
54eb5dacddc443db0752529a69a9c48dab1b129a18Scott Main
55db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<img src="{@docRoot}images/ui/dialogs.png" />
56eb5dacddc443db0752529a69a9c48dab1b129a18Scott Main
57db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>The {@link android.app.Dialog} class is the base class for dialogs, but you
58db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainshould avoid instantiating {@link android.app.Dialog} directly.
59db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainInstead, use one of the following subclasses:</p>
60db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<dl>
61db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dt>{@link android.app.AlertDialog}</dt>
62db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dd>A dialog that can show a title, up to three buttons, a list of
63db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    selectable items, or a custom layout.</dd>
64db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dt>{@link android.app.DatePickerDialog} or {@link android.app.TimePickerDialog}</dt>
65db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dd>A dialog with a pre-defined UI that allows the user to select a date or time.</dd>
66db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</dl>
67ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
68db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<div class="sidebox">
69db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h2>Avoid ProgressDialog</h2>
70db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Android includes another dialog class called
71db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.app.ProgressDialog} that shows a dialog with a progress bar. However, if you
72db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainneed to indicate loading or indeterminate progress, you should instead follow the design
73db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainguidelines for <a href="{@docRoot}design/building-blocks/progress.html">Progress &amp;
74db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainActivity</a> and use a {@link android.widget.ProgressBar} in your layout.</p>
75db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</div>
76ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
77db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>These classes define the style and structure for your dialog, but you should
78db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainuse a {@link android.support.v4.app.DialogFragment} as a container for your dialog.
79db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainThe {@link android.support.v4.app.DialogFragment} class provides all the controls you
80db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainneed to create your dialog and manage its appearance, instead of calling methods
81db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainon the {@link android.app.Dialog} object.</p>
82db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
83db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Using {@link android.support.v4.app.DialogFragment} to manage the dialog
84db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainensures that it correctly handles lifecycle events
85db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainsuch as when the user presses the <em>Back</em> button or rotates the screen. The {@link
86db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment} class also allows you to reuse the dialog's UI as an
87db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainembeddable component in a larger UI, just like a traditional {@link
88db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.Fragment} (such as when you want the dialog UI to appear differently
89db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainon large and small screens).</p>
90db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
91db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>The following sections in this guide describe how to use a {@link
92db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment} in combination with an {@link android.app.AlertDialog}
93db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainobject. If you'd like to create a date or time picker, you should instead read the
94db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> guide.</p>
95db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
96db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="note"><strong>Note:</strong>
97db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainBecause the {@link android.app.DialogFragment} class was originally added with
98db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainAndroid 3.0 (API level 11), this document describes how to use the {@link
99db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment} class that's provided with the <a
1004e2c9dc74b74b1ba10793177475a599f75fbb18bScott Mainhref="{@docRoot}tools/support-library/index.html">Support Library</a>. By adding this library
101db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainto your app, you can use {@link android.support.v4.app.DialogFragment} and a variety of other
102db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainAPIs on devices running Android 1.6 or higher. If the minimum version your app supports
103db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainis API level 11 or higher, then you can use the framework version of {@link
104db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.app.DialogFragment}, but be aware that the links in this document are for the support
105db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainlibrary APIs. When using the support library,
106db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainbe sure that you import <code>android.support.v4.app.DialogFragment</code>
107db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainclass and <em>not</em> <code>android.app.DialogFragment</code>.</p>
108db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
109db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
110db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h2 id="DialogFragment">Creating a Dialog Fragment</h2>
111db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
112db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>You can accomplish a wide variety of dialog designs&mdash;including
113db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maincustom layouts and those described in the <a
114db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainhref="{@docRoot}design/building-blocks/dialogs.html">Dialogs</a>
115db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maindesign guide&mdash;by extending
116db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.support.v4.app.DialogFragment} and creating a {@link android.app.AlertDialog}
117db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainin the {@link android.support.v4.app.DialogFragment#onCreateDialog
118db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainonCreateDialog()} callback method.</p>
119db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
120db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>For example, here's a basic {@link android.app.AlertDialog} that's managed within
121db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maina {@link android.support.v4.app.DialogFragment}:</p>
122ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
123ce878bef710c6d1e6881949c82b1293fb8435715Scott Main<pre>
12407322102e5f1877bc2860d2c72eeeb41839d6beaScott Mainpublic class FireMissilesDialogFragment extends DialogFragment {
125db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &#64;Override
126db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    public Dialog onCreateDialog(Bundle savedInstanceState) {
127db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // Use the Builder class for convenient dialog construction
128db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
129db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        builder.setMessage(R.string.dialog_fire_missiles)
130db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
131db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   public void onClick(DialogInterface dialog, int id) {
132db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                       // FIRE ZE MISSILES!
133db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   }
134db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               })
135db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
136db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   public void onClick(DialogInterface dialog, int id) {
137db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                       // User cancelled the dialog
138db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   }
139db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               });
140db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // Create the AlertDialog object and return it
141db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        return builder.create();
142ce878bef710c6d1e6881949c82b1293fb8435715Scott Main    }
143ce878bef710c6d1e6881949c82b1293fb8435715Scott Main}
144ce878bef710c6d1e6881949c82b1293fb8435715Scott Main</pre>
145ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
146db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<div class="figure" style="width:290px;margin:0 0 0 20px">
147db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<img src="{@docRoot}images/ui/dialog_buttons.png" alt="" />
148db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="img-caption"><strong>Figure 1.</strong>
149db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainA dialog with a message and two action buttons.</p>
150db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</div>
151ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
152db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Now, when you create an instance of this class and call {@link
153db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment#show show()} on that object, the dialog appears as
154db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainshown in figure 1.</p>
155ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
156db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>The next section describes more about using the {@link android.app.AlertDialog.Builder}
157db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainAPIs to create the dialog.</p>
158ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
159db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Depending on how complex your dialog is, you can implement a variety of other callback
160db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainmethods in the {@link android.support.v4.app.DialogFragment}, including all the basic
161db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<a href="{@docRoot}guide/components/fragments.html#Lifecycle">fragment lifecycle methods</a>.
162ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
163ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
164ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
165ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
166ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
167db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h2 id="AlertDialog">Building an Alert Dialog</h2>
168db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
169db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
170db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>The {@link android.app.AlertDialog} class allows you to build a variety of dialog designs and
171db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainis often the only dialog class you'll need.
172db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainAs shown in figure 2, there are three regions of an alert dialog:</p>
173db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
174db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<div class="figure" style="width:311px;margin-top:0">
175db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<img src="{@docRoot}images/ui/dialogs_regions.png" alt="" style="margin-bottom:0"/>
176db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="img-caption"><strong>Figure 2.</strong> The layout of a dialog.</p>
177db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</div>
178ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
179db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<ol>
180db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<li><b>Title</b>
181db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <p>This is optional and should be used only when the content area
182db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  is occupied by a detailed message, a list, or custom layout. If you need to state
183db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  a simple message or question (such as the dialog in figure 1), you don't need a title.</li>
184db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<li><b>Content area</b>
185db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <p>This can display a message, a list, or other custom layout.</p></li>
186db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<li><b>Action buttons</b>
187db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <p>There should be no more than three action buttons in a dialog.</p></li>
188db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</ol>
189ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
190db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>The {@link android.app.AlertDialog.Builder}
191db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainclass provides APIs that allow you to create an {@link android.app.AlertDialog}
192db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainwith these kinds of content, including a custom layout.</p>
193db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
194db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>To build an {@link android.app.AlertDialog}:</p>
195ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
196ce878bef710c6d1e6881949c82b1293fb8435715Scott Main<pre>
197db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<b>// 1. Instantiate an {@link android.app.AlertDialog.Builder} with its constructor</b>
198db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainAlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
199db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
200db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<b>// 2. Chain together various setter methods to set the dialog characteristics</b>
201db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainbuilder.setMessage(R.string.dialog_message)
202db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main       .setTitle(R.string.dialog_title);
203db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
204db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<b>// 3. Get the {@link android.app.AlertDialog} from {@link android.app.AlertDialog.Builder#create()}</b>
205db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainAlertDialog dialog = builder.create();
206db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
207db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
208db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>The following topics show how to define various dialog attributes using the
209db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.app.AlertDialog.Builder} class.</p>
210db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
211db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
212db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
213db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
214db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h3 id="AddingButtons">Adding buttons</h3>
215db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
216db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>To add action buttons like those in figure 2,
217db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maincall the {@link android.app.AlertDialog.Builder#setPositiveButton setPositiveButton()} and
218db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.app.AlertDialog.Builder#setNegativeButton setNegativeButton()} methods:</p>
219db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
220db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre style="clear:right">
221db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainAlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
222db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main// Add the buttons
223db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainbuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
224ce878bef710c6d1e6881949c82b1293fb8435715Scott Main           public void onClick(DialogInterface dialog, int id) {
225db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               // User clicked OK button
226ce878bef710c6d1e6881949c82b1293fb8435715Scott Main           }
227db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main       });
228db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainbuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
229ce878bef710c6d1e6881949c82b1293fb8435715Scott Main           public void onClick(DialogInterface dialog, int id) {
230db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               // User cancelled the dialog
231ce878bef710c6d1e6881949c82b1293fb8435715Scott Main           }
232ce878bef710c6d1e6881949c82b1293fb8435715Scott Main       });
233db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main// Set other dialog properties
234db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main...
235db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
236db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main// Create the AlertDialog
237db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainAlertDialog dialog = builder.create();
238ce878bef710c6d1e6881949c82b1293fb8435715Scott Main</pre>
239ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
240db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>The <code>set...Button()</code> methods require a title for the button (supplied
241db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainby a <a href="{@docRoot}guide/topics/resources/string-resource.html">string resource</a>) and a 
242ce878bef710c6d1e6881949c82b1293fb8435715Scott Main{@link android.content.DialogInterface.OnClickListener} that defines the action to take 
243db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainwhen the user presses the button.</p>
244ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
245db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>There are three different action buttons you can add:</p>
246db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<dl>
247db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dt>Positive</dt>
248db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dd>You should use this to accept and continue with the action (the "OK" action).</dd>
249db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dt>Negative</dt>
250db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dd>You should use this to cancel the action.</dd>
251db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dt>Neutral</dt>
252db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  <dd>You should use this when the user may not want to proceed with the action,
253db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  but doesn't necessarily want to cancel. It appears between the positive and negative
254db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  buttons. For example, the action might be "Remind me later."</dd> 
255db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</dl>
256ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
257db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>You can add only one of each button type to an {@link
258db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.app.AlertDialog}. That is, you cannot have more than one "positive" button.</p>
259ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
260ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
261ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
262db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<div class="figure" style="width:290px;margin:0 0 0 40px">
263db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<img src="{@docRoot}images/ui/dialog_list.png" alt="" />
264db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="img-caption"><strong>Figure 3.</strong>
265db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainA dialog with a title and list.</p>
266db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</div>
267ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
268db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h3 id="AddingAList">Adding a list</h3>
269ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
270db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>There are three kinds of lists available with the {@link android.app.AlertDialog} APIs:</p>
271db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<ul>
272db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<li>A traditional single-choice list</li>
273db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<li>A persistent single-choice list (radio buttons)</li>
274db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<li>A persistent multiple-choice list (checkboxes)</li>
275db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</ul>
276ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
277db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>To create a single-choice list like the one in figure 3, 
278db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainuse the {@link android.app.AlertDialog.Builder#setItems setItems()} method:</p>
279db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
280db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre style="clear:right">
281db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&#64;Override
282db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainpublic Dialog onCreateDialog(Bundle savedInstanceState) {
283db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
2847a8acfa68e477d05ca8beeabc7cdd732639f18f4Ricardo Cervera    builder.setTitle(R.string.pick_color)
285db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
286db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               public void onClick(DialogInterface dialog, int which) {
287db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               // The 'which' argument contains the index position
288db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               // of the selected item
289db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           }
290db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    });
291db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    return builder.create();
292db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main}
293db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
294ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
295db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Because the list appears in the dialog's content area,
296db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainthe dialog cannot show both a message and a list and you should set a title for the
297db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maindialog with {@link android.app.AlertDialog.Builder#setTitle setTitle()}. 
298db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainTo specify the items for the list, call {@link
299db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.app.AlertDialog.Builder#setItems setItems()}, passing an array.
300db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainAlternatively, you can specify a list using {@link
301db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.app.AlertDialog.Builder#setAdapter setAdapter()}. This allows you to back the list
302db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainwith dynamic data (such as from a database) using a {@link android.widget.ListAdapter}.</p>
303db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
304db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>If you choose to back your list with a {@link android.widget.ListAdapter},
305db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainalways use a {@link android.support.v4.content.Loader} so that the content loads
306db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainasynchronously. This is described further in
307db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<a href="{@docRoot}guide/topics/ui/declaring-layout.html#AdapterViews">Building Layouts
308db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainwith an Adapter</a> and the <a href="{@docRoot}guide/components/loaders.html">Loaders</a>
309db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainguide.</p>
310db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
311db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="note"><strong>Note:</strong> By default, touching a list item dismisses the dialog,
312db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainunless you're using one of the following persistent choice lists.</p>
313db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
314db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<div class="figure" style="width:290px;margin:-30px 0 0 40px">
315db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<img src="{@docRoot}images/ui/dialog_checkboxes.png" />
316db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="img-caption"><strong>Figure 4.</strong>
317db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainA list of multiple-choice items.</p>
318db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</div>
319ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
320ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
321db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h4 id="Checkboxes">Adding a persistent multiple-choice or single-choice list</h4>
322ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
323db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>To add a list of multiple-choice items (checkboxes) or 
324db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainsingle-choice items (radio buttons), use the
325ce878bef710c6d1e6881949c82b1293fb8435715Scott Main{@link android.app.AlertDialog.Builder#setMultiChoiceItems(Cursor,String,String,
326db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainDialogInterface.OnMultiChoiceClickListener) setMultiChoiceItems()} or 
327ce878bef710c6d1e6881949c82b1293fb8435715Scott Main{@link android.app.AlertDialog.Builder#setSingleChoiceItems(int,int,DialogInterface.OnClickListener) 
328db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainsetSingleChoiceItems()} methods, respectively.</p>
329db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
330db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>For example, here's how you can create a multiple-choice list like the
331db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainone shown in figure 4 that saves the selected
332db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainitems in an {@link java.util.ArrayList}:</p>
333db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
334db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre style="clear:right">
335db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&#64;Override
336db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainpublic Dialog onCreateDialog(Bundle savedInstanceState) {
337db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    mSelectedItems = new ArrayList();  // Where we track the selected items
338db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
339db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    // Set the dialog title
340db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    builder.setTitle(R.string.pick_toppings)
341db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    // Specify the list array, the items to be selected by default (null for none),
342db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    // and the listener through which to receive callbacks when items are selected
343db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           .setMultiChoiceItems(R.array.toppings, null,
344db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                      new DialogInterface.OnMultiChoiceClickListener() {
345db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               &#64;Override
346db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               public void onClick(DialogInterface dialog, int which,
347db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                       boolean isChecked) {
348db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   if (isChecked) {
349db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                       // If the user checked the item, add it to the selected items
350db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                       mSelectedItems.add(which);
351db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   } else if (mSelectedItems.contains(which)) {
352db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                       // Else, if the item is already in the array, remove it 
353db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                       mSelectedItems.remove(Integer.valueOf(which));
354db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   }
355db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               }
356db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           })
357db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    // Set the action buttons
358db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
359db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               &#64;Override
360db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               public void onClick(DialogInterface dialog, int id) {
361db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   // User clicked OK, so save the mSelectedItems results somewhere
362db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   // or return them to the component that opened the dialog
363db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   ...
364db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               }
365db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           })
366db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
367db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               &#64;Override
368db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               public void onClick(DialogInterface dialog, int id) {
369db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   ...
370db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               }
371db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           });
372db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
373db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    return builder.create();
374db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main}
375db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
376db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
377db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Although both a traditional list and a list with radio buttons
378db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainprovide a "single choice" action, you should use {@link
379db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.app.AlertDialog.Builder#setSingleChoiceItems(int,int,DialogInterface.OnClickListener) 
380db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainsetSingleChoiceItems()} if you want to persist the user's choice.
381db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainThat is, if opening the dialog again later should indicate what the user's current choice is,
382db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainthen you create a list with radio buttons.</p>
383db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
384db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
385ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
386ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
387ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
388db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h3 id="CustomLayout">Creating a Custom Layout</h3>
389ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
390db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<div class="figure" style="width:290px;margin:-30px 0 0 40px">
391db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<img src="{@docRoot}images/ui/dialog_custom.png" alt="" />
392db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="img-caption"><strong>Figure 5.</strong> A custom dialog layout.</p>
393db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</div>
394db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
395db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>If you want a custom layout in a dialog, create a layout and add it to an
396db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.app.AlertDialog} by calling {@link
397db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.app.AlertDialog.Builder#setView setView()} on your {@link
398db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.app.AlertDialog.Builder} object.</p>
399db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
400db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>By default, the custom layout fills the dialog window, but you can still
401db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainuse {@link android.app.AlertDialog.Builder} methods to add buttons and a title.</p>
402ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
403db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>For example, here's the layout file for the dialog in Figure 5:</p>
404ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
405db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p style="clear:right" class="code-caption">res/layout/dialog_signin.xml</p>
406db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre>
407db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
408db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    android:orientation="vertical"
409db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    android:layout_width="wrap_content"
410db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    android:layout_height="wrap_content">
411db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &lt;ImageView
412db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:src="@drawable/header_logo"
413db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_width="match_parent"
414db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_height="64dp"
415db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:scaleType="center"
416db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:background="#FFFFBB33"
417db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:contentDescription="@string/app_name" />
418db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &lt;EditText
419db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:id="@+id/username"
420db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:inputType="textEmailAddress"
421db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_width="match_parent"
422db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_height="wrap_content"
423db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_marginTop="16dp"
424db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_marginLeft="4dp"
425db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_marginRight="4dp"
426db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_marginBottom="4dp"
427db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:hint="@string/username" />
428db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &lt;EditText
429db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:id="@+id/password"
430db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:inputType="textPassword"
431db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_width="match_parent"
432db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_height="wrap_content"
433db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_marginTop="4dp"
434db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_marginLeft="4dp"
435db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_marginRight="4dp"
436db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:layout_marginBottom="16dp"
437db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:fontFamily="sans-serif"
438db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        android:hint="@string/password"/>
439db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;/LinearLayout>
440db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
441ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
442db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="note"><strong>Tip:</strong> By default, when you set an {@link android.widget.EditText}
443db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainelement to use the {@code "textPassword"} input type, the font family is set to monospace, so
444db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainyou should change its font family to {@code "sans-serif"} so that both text fields use
445db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maina matching font style.</p>
446ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
447db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>To inflate the layout in your {@link android.support.v4.app.DialogFragment},
448db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainget a {@link android.view.LayoutInflater} with 
449db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.app.Activity#getLayoutInflater()} and call
450db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.view.LayoutInflater#inflate inflate()}, where the first parameter
451db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainis the layout resource ID and the second parameter is a parent view for the layout.
452db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainYou can then call {@link android.app.AlertDialog#setView setView()}
453db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainto place the layout in the dialog.</p>
454ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
455ce878bef710c6d1e6881949c82b1293fb8435715Scott Main<pre>
456db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&#64;Override
457db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainpublic Dialog onCreateDialog(Bundle savedInstanceState) {
458db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
459db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    // Get the layout inflater
460db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    LayoutInflater inflater = getActivity().getLayoutInflater();
461db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
462db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    // Inflate and set the layout for the dialog
463db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    // Pass null as the parent view because its going in the dialog layout
464db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
465db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    // Add action buttons
466db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
467db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               &#64;Override
468db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               public void onClick(DialogInterface dialog, int id) {
469db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   // sign in the user ...
470db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               }
471db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           })
472db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
473db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               public void onClick(DialogInterface dialog, int id) {
47407322102e5f1877bc2860d2c72eeeb41839d6beaScott Main                   LoginDialogFragment.this.getDialog().cancel();
475db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               }
476db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main           });      
477db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    return builder.create();
478db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main}
479ce878bef710c6d1e6881949c82b1293fb8435715Scott Main</pre>
480ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
481db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<div class="note">
482db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p><strong>Tip:</strong> If you want a custom dialog,
483db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainyou can instead display an {@link android.app.Activity} as a dialog
484db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maininstead of using the {@link android.app.Dialog} APIs. Simply create an activity and set its theme to
485db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.R.style#Theme_Holo_Dialog Theme.Holo.Dialog}
486db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainin the <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code
487db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;activity&gt;}</a> manifest element:</p>
488ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
489db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre>
490db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;activity android:theme="&#64;android:style/Theme.Holo.Dialog" >
491db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
492db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>That's it. The activity now displays in a dialog window instead of fullscreen.</p>
493db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</div>
494ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
495ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
496ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
497db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h2 id="PassingEvents">Passing Events Back to the Dialog's Host</h2>
498ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
499db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>When the user touches one of the dialog's action buttons or selects an item from its list,
500db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainyour {@link android.support.v4.app.DialogFragment} might perform the necessary
501db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainaction itself, but often you'll want to deliver the event to the activity or fragment that
50207322102e5f1877bc2860d2c72eeeb41839d6beaScott Mainopened the dialog. To do this, define an interface with a method for each type of click event.
50307322102e5f1877bc2860d2c72eeeb41839d6beaScott MainThen implement that interface in the host component that will
504db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainreceive the action events from the dialog.</p>
505ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
506db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>For example, here's a {@link android.support.v4.app.DialogFragment} that defines an
507db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maininterface through which it delivers the events back to the host activity:</p>
508ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
509ce878bef710c6d1e6881949c82b1293fb8435715Scott Main<pre>
51007322102e5f1877bc2860d2c72eeeb41839d6beaScott Mainpublic class NoticeDialogFragment extends DialogFragment {
511db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    
512db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    /* The activity that creates an instance of this dialog fragment must
513db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main     * implement this interface in order to receive event callbacks.
514db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main     * Each method passes the DialogFragment in case the host needs to query it. */
515db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    public interface NoticeDialogListener {
516db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        public void onDialogPositiveClick(DialogFragment dialog);
517db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        public void onDialogNegativeClick(DialogFragment dialog);
518db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    }
519db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    
520db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    // Use this instance of the interface to deliver action events
52107322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    NoticeDialogListener mListener;
52207322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    
52307322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
52407322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    &#64;Override
52507322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    public void onAttach(Activity activity) {
52607322102e5f1877bc2860d2c72eeeb41839d6beaScott Main        super.onAttach(activity);
527db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // Verify that the host activity implements the callback interface
528db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        try {
52907322102e5f1877bc2860d2c72eeeb41839d6beaScott Main            // Instantiate the NoticeDialogListener so we can send events to the host
530db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main            mListener = (NoticeDialogListener) activity;
531db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        } catch (ClassCastException e) {
532db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main            // The activity doesn't implement the interface, throw exception
533db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main            throw new ClassCastException(activity.toString()
534db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                    + " must implement NoticeDialogListener");
535db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        }
536db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    }
537db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    ...
538db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main}
539ce878bef710c6d1e6881949c82b1293fb8435715Scott Main</pre>
540ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
54107322102e5f1877bc2860d2c72eeeb41839d6beaScott Main<p>The activity hosting the dialog creates an instance of the dialog
54207322102e5f1877bc2860d2c72eeeb41839d6beaScott Mainwith the dialog fragment's constructor and receives the dialog's
543db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainevents through an implementation of the {@code NoticeDialogListener} interface:</p>
544ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
545ce878bef710c6d1e6881949c82b1293fb8435715Scott Main<pre>
546db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainpublic class MainActivity extends FragmentActivity
54707322102e5f1877bc2860d2c72eeeb41839d6beaScott Main                          implements NoticeDialogFragment.NoticeDialogListener{
548db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    ...
549db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    
550db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    public void showNoticeDialog() {
551db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // Create an instance of the dialog fragment and show it
55207322102e5f1877bc2860d2c72eeeb41839d6beaScott Main        DialogFragment dialog = new NoticeDialogFragment();
55307322102e5f1877bc2860d2c72eeeb41839d6beaScott Main        dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
554ce878bef710c6d1e6881949c82b1293fb8435715Scott Main    }
555db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
55607322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    // The dialog fragment receives a reference to this Activity through the
55707322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    // Fragment.onAttach() callback, which it uses to call the following methods
55807322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    // defined by the NoticeDialogFragment.NoticeDialogListener interface
559db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &#64;Override
560db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    public void onDialogPositiveClick(DialogFragment dialog) {
561db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // User touched the dialog's positive button
562db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        ...
563ce878bef710c6d1e6881949c82b1293fb8435715Scott Main    }
564ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
5659316232130fe831c998c9d7be9cf67294eccdcaeJoe Onorato    &#64;Override
566db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    public void onDialogNegativeClick(DialogFragment dialog) {
567db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // User touched the dialog's negative button
568db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        ...
5696e1df0e981c8c588e411b8ad6e55554fe4815655Gilles Debunne    }
570db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main}
571db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
5726e1df0e981c8c588e411b8ad6e55554fe4815655Gilles Debunne
573db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Because the host activity implements the {@code NoticeDialogListener}&mdash;which is
57407322102e5f1877bc2860d2c72eeeb41839d6beaScott Mainenforced by the {@link android.support.v4.app.Fragment#onAttach onAttach()}
57507322102e5f1877bc2860d2c72eeeb41839d6beaScott Maincallback method shown above&mdash;the dialog fragment can use the
576db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maininterface callback methods to deliver click events to the activity:</p>
577db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
578db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre>
57907322102e5f1877bc2860d2c72eeeb41839d6beaScott Mainpublic class NoticeDialogFragment extends DialogFragment {
580db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    ...
581db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
582db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &#64;Override
583db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    public Dialog onCreateDialog(Bundle savedInstanceState) {
584db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // Build the dialog and set up the button click handlers
585db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
586db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        builder.setMessage(R.string.dialog_fire_missiles)
587db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
588db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   public void onClick(DialogInterface dialog, int id) {
589db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                       // Send the positive button event back to the host activity
59007322102e5f1877bc2860d2c72eeeb41839d6beaScott Main                       mListener.onDialogPositiveClick(NoticeDialogFragment.this);
591db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   }
592db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               })
593db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
594db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   public void onClick(DialogInterface dialog, int id) {
595db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                       // Send the negative button event back to the host activity
5962e7c8e952cbc30fbef083c154662e7c3b6fa253aDavid Friedman                       mListener.onDialogNegativeClick(NoticeDialogFragment.this);
597db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   }
598db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main               });
599db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        return builder.create();
600ce878bef710c6d1e6881949c82b1293fb8435715Scott Main    }
601ce878bef710c6d1e6881949c82b1293fb8435715Scott Main}
602ce878bef710c6d1e6881949c82b1293fb8435715Scott Main</pre>
603ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
604ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
605ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
606db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h2 id="ShowingADialog">Showing a Dialog</h2>
607ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
608db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>When you want to show your dialog, create an instance of your {@link
609db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment} and call {@link android.support.v4.app.DialogFragment#show
610db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainshow()}, passing the {@link android.support.v4.app.FragmentManager} and a tag name
611db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainfor the dialog fragment.</p>
612db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
613db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>You can get the {@link android.support.v4.app.FragmentManager} by calling
614db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.support.v4.app.FragmentActivity#getSupportFragmentManager()} from
615db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainthe {@link android.support.v4.app.FragmentActivity} or {@link
616db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.Fragment#getFragmentManager()} from a {@link
617db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.Fragment}. For example:</p>
618ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
619ce878bef710c6d1e6881949c82b1293fb8435715Scott Main<pre>
620db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainpublic void confirmFireMissiles() {
62107322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    DialogFragment newFragment = new FireMissilesDialogFragment();
622db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    newFragment.show(getSupportFragmentManager(), "missiles");
623db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main}
624ce878bef710c6d1e6881949c82b1293fb8435715Scott Main</pre>
625ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
626db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>The second argument, {@code "missiles"}, is a unique tag name that the system uses to save
627db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainand restore the fragment state when necessary. The tag also allows you to get a handle to
628db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainthe fragment by calling {@link android.support.v4.app.FragmentManager#findFragmentByTag
629db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainfindFragmentByTag()}.</p>
630db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
631db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
632db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
633db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
634db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h2 id="FullscreenDialog">Showing a Dialog Fullscreen or as an Embedded Fragment</h2>
635db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
636db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>You might have a UI design in which you want a piece of the UI to appear as a dialog in some
637db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainsituations, but as a full screen or embedded fragment in others (perhaps depending on whether
638db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainthe device is a large screen or small screen). The {@link android.support.v4.app.DialogFragment}
639db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainclass offers you this flexibility because it can still behave as an embeddable {@link
640db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.Fragment}.</p>
641db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
642db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>However, you cannot use {@link android.app.AlertDialog.Builder AlertDialog.Builder}
643db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainor other {@link android.app.Dialog} objects to build the dialog in this case. If
644db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainyou want the {@link android.support.v4.app.DialogFragment} to be
645db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainembeddable, you must define the dialog's UI in a layout, then load the layout in the
646db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.support.v4.app.DialogFragment#onCreateView
647db705ef67bebe00deaacc12201c5d92ae8b3f851Scott MainonCreateView()} callback.</p>
648db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
649db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Here's an example {@link android.support.v4.app.DialogFragment} that can appear as either a
650db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maindialog or an embeddable fragment (using a layout named <code>purchase_items.xml</code>):</p>
651db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
652ce878bef710c6d1e6881949c82b1293fb8435715Scott Main<pre>
65307322102e5f1877bc2860d2c72eeeb41839d6beaScott Mainpublic class CustomDialogFragment extends DialogFragment {
654db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    /** The system calls this to get the DialogFragment's layout, regardless
655db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        of whether it's being displayed as a dialog or an embedded fragment. */
656db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &#64;Override
657db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    public View onCreateView(LayoutInflater inflater, ViewGroup container,
658db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main            Bundle savedInstanceState) {
659db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // Inflate the layout to use as dialog or embedded fragment
660db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        return inflater.inflate(R.layout.purchase_items, container, false);
661db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    }
662db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main  
663db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    /** The system calls this only when creating the layout in a dialog. */
664db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &#64;Override
665db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    public Dialog onCreateDialog(Bundle savedInstanceState) {
666db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // The only reason you might override this method when using onCreateView() is
667db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // to modify any dialog characteristics. For example, the dialog includes a
668db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // title by default, but your custom layout might not need it. So here you can
669db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // remove the dialog title, but you must call the superclass to get the Dialog.
670db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        Dialog dialog = super.onCreateDialog(savedInstanceState);
671db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
672db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        return dialog;
673db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    }
674db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main}
675db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
676ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
677db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>And here's some code that decides whether to show the fragment as a dialog
678db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainor a fullscreen UI, based on the screen size:</p>
679ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
680db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre>
681db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainpublic void showDialog() {
682db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    FragmentManager fragmentManager = getSupportFragmentManager();
68307322102e5f1877bc2860d2c72eeeb41839d6beaScott Main    CustomDialogFragment newFragment = new CustomDialogFragment();
684db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    
685db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    if (mIsLargeLayout) {
686db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // The device is using a large layout, so show the fragment as a dialog
687db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        newFragment.show(fragmentManager, "dialog");
688db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    } else {
689db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // The device is smaller, so show the fragment fullscreen
690db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        FragmentTransaction transaction = fragmentManager.beginTransaction();
691db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // For a little polish, specify a transition animation
692db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
693db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // To make it fullscreen, use the 'content' root view as the container
694db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        // for the fragment, which is always the root view for the activity
695db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main        transaction.add(android.R.id.content, newFragment)
696db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main                   .addToBackStack(null).commit();
697db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    }
698db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main}
699ce878bef710c6d1e6881949c82b1293fb8435715Scott Main</pre>
700ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
701db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>For more information about performing fragment transactions, see the
702db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<a href="{@docRoot}guide/components/fragments.html">Fragments</a> guide.</p>
703ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
704db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>In this example, the <code>mIsLargeLayout</code> boolean specifies whether the current device
705db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainshould use the app's large layout design (and thus show this fragment as a dialog, rather
706db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainthan fullscreen). The best way to set this kind of boolean is to declare a
707db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<a href="{@docRoot}guide/topics/resources/more-resources.html#Bool">bool resource value</a>
708db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainwith an <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources"
709db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main>alternative resource</a> value for different screen sizes. For example, here are two
710db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainversions of the bool resource for different screen sizes:</p>
711ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
712db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="code-caption">res/values/bools.xml</p>
713db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre>
714db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;!-- Default boolean values -->
715db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;resources>
716db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &lt;bool name="large_layout">false&lt;/bool>
717db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;/resources>
718db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
719db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
720db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="code-caption">res/values-large/bools.xml</p>
721db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre>
722db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;!-- Large screen boolean values -->
723db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;resources>
724db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    &lt;bool name="large_layout">true&lt;/bool>
725db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;/resources>
726db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
727db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
728db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Then you can initialize the {@code mIsLargeLayout} value during the activity's
729db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.app.Activity#onCreate onCreate()} method:</p>
730db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
731db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<pre>
732db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainboolean mIsLargeLayout;
733db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
734db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&#64;Override
735db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainpublic void onCreate(Bundle savedInstanceState) {
736db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    super.onCreate(savedInstanceState);
737db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    setContentView(R.layout.activity_main);
738db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
739db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main    mIsLargeLayout = getResources().getBoolean(R.bool.large_layout);
740db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main}
741db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main</pre>
742db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
743db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
744db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
745db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h3 id="ActivityAsDialog">Showing an activity as a dialog on large screens</h3>
746db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
747db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>Instead of showing a dialog as a fullscreen UI when on small screens, you can accomplish
748db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainthe same result by showing an {@link android.app.Activity} as a dialog when on
749db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainlarge screens. Which approach you choose depends on your app design, but
750db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainshowing an activity as a dialog is often useful when your app is already designed for small
751db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainscreens and you'd like to improve the experience on tablets by showing a short-lived activity
752db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainas a dialog.</p>
753db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
754db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>To show an activity as a dialog only when on large screens,
755db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainapply the {@link android.R.style#Theme_Holo_DialogWhenLarge Theme.Holo.DialogWhenLarge}
756db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maintheme to the <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code
757db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;activity&gt;}</a> manifest element:</p>
758ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
759ce878bef710c6d1e6881949c82b1293fb8435715Scott Main<pre>
760db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main&lt;activity android:theme="&#64;android:style/Theme.Holo.DialogWhenLarge" >
761ce878bef710c6d1e6881949c82b1293fb8435715Scott Main</pre>
762ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
763db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>For more information about styling your activities with themes, see the <a
764db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainhref="{@docRoot}guide/topics/ui/themes.html">Styles and Themes</a> guide.</p>
765db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
766ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
767ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
768db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<h2 id="DismissingADialog">Dismissing a Dialog</h2>
769db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
770db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>When the user touches any of the action buttons created with an
771db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.app.AlertDialog.Builder}, the system dismisses the dialog for you.</p>
772db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
773db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>The system also dismisses the dialog when the user touches an item in a dialog list, except
774db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainwhen the list uses radio buttons or checkboxes. Otherwise, you can manually dismiss your dialog
775db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainby calling {@link android.support.v4.app.DialogFragment#dismiss()} on your {@link
776db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment}.</p>
777db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
778db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>In case you need to perform certain
779db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainactions when the dialog goes away, you can implement the {@link
780db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment#onDismiss onDismiss()} method in your {@link
781db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment}.</p>
782db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
783db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>You can also <em>cancel</em> a dialog. This is a special event that indicates the user
784db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainexplicitly left the dialog without completing the task. This occurs if the user presses the 
785db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<em>Back</em> button, touches the screen outside the dialog area,
786db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainor if you explicitly call {@link android.app.Dialog#cancel()} on the {@link
787db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.app.Dialog} (such as in response to a "Cancel" button in the dialog).</p>
788db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
789db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p>As shown in the example above, you can respond to the cancel event by implementing
790db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.support.v4.app.DialogFragment#onCancel onCancel()} in your {@link
791db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment} class.</p>
792db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main
793db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<p class="note"><strong>Note:</strong> The system calls
794db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main{@link android.support.v4.app.DialogFragment#onDismiss onDismiss()} upon each event that
795db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maininvokes the {@link android.support.v4.app.DialogFragment#onCancel onCancel()} callback. However,
796db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainif you call {@link android.app.Dialog#dismiss Dialog.dismiss()} or {@link
797db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainandroid.support.v4.app.DialogFragment#dismiss DialogFragment.dismiss()},
798db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainthe system calls {@link android.support.v4.app.DialogFragment#onDismiss onDismiss()} <em>but
799db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Mainnot</em> {@link android.support.v4.app.DialogFragment#onCancel onCancel()}. So you should generally
800db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Maincall {@link android.support.v4.app.DialogFragment#dismiss dismiss()} when the user presses the
801db705ef67bebe00deaacc12201c5d92ae8b3f851Scott Main<em>positive</em> button in your dialog in order to remove the dialog from view.</p>
802ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
803ce878bef710c6d1e6881949c82b1293fb8435715Scott Main
804