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