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