Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.app;
     18 
     19 import android.app.Activity;
     20 import android.app.AlertDialog;
     21 import android.app.Dialog;
     22 import android.app.ProgressDialog;
     23 import android.content.DialogInterface;
     24 import android.os.Bundle;
     25 import android.os.Handler;
     26 import android.os.Message;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.View.OnClickListener;
     30 import android.widget.Button;
     31 import android.widget.Toast;
     32 import android.database.Cursor;
     33 import android.provider.ContactsContract;
     34 
     35 import com.example.android.apis.R;
     36 
     37 /**
     38  * Example of how to use an {@link android.app.AlertDialog}.
     39  * <h3>AlertDialogSamples</h3>
     40 
     41 <p>This demonstrates the different ways the AlertDialog can be used.</p>
     42 
     43 <h4>Demo</h4>
     44 App/Dialog/Alert Dialog
     45 
     46 <h4>Source files</h4>
     47  * <table class="LinkTable">
     48  *         <tr>
     49  *             <td >src/com.example.android.apis/app/AlertDialogSamples.java</td>
     50  *             <td >The Alert Dialog Samples implementation</td>
     51  *         </tr>
     52  *         <tr>
     53  *             <td >/res/any/layout/alert_dialog.xml</td>
     54  *             <td >Defines contents of the screen</td>
     55  *         </tr>
     56  * </table>
     57  */
     58 public class AlertDialogSamples extends Activity {
     59     private static final int DIALOG_YES_NO_MESSAGE = 1;
     60     private static final int DIALOG_YES_NO_LONG_MESSAGE = 2;
     61     private static final int DIALOG_LIST = 3;
     62     private static final int DIALOG_PROGRESS = 4;
     63     private static final int DIALOG_SINGLE_CHOICE = 5;
     64     private static final int DIALOG_MULTIPLE_CHOICE = 6;
     65     private static final int DIALOG_TEXT_ENTRY = 7;
     66     private static final int DIALOG_MULTIPLE_CHOICE_CURSOR = 8;
     67 
     68     private static final int MAX_PROGRESS = 100;
     69 
     70     private ProgressDialog mProgressDialog;
     71     private int mProgress;
     72     private Handler mProgressHandler;
     73 
     74     @Override
     75     protected Dialog onCreateDialog(int id) {
     76         switch (id) {
     77         case DIALOG_YES_NO_MESSAGE:
     78             return new AlertDialog.Builder(AlertDialogSamples.this)
     79                 .setIcon(R.drawable.alert_dialog_icon)
     80                 .setTitle(R.string.alert_dialog_two_buttons_title)
     81                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
     82                     public void onClick(DialogInterface dialog, int whichButton) {
     83 
     84                         /* User clicked OK so do some stuff */
     85                     }
     86                 })
     87                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
     88                     public void onClick(DialogInterface dialog, int whichButton) {
     89 
     90                         /* User clicked Cancel so do some stuff */
     91                     }
     92                 })
     93                 .create();
     94         case DIALOG_YES_NO_LONG_MESSAGE:
     95             return new AlertDialog.Builder(AlertDialogSamples.this)
     96                 .setIcon(R.drawable.alert_dialog_icon)
     97                 .setTitle(R.string.alert_dialog_two_buttons_msg)
     98                 .setMessage(R.string.alert_dialog_two_buttons2_msg)
     99                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
    100                     public void onClick(DialogInterface dialog, int whichButton) {
    101 
    102                         /* User clicked OK so do some stuff */
    103                     }
    104                 })
    105                 .setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {
    106                     public void onClick(DialogInterface dialog, int whichButton) {
    107 
    108                         /* User clicked Something so do some stuff */
    109                     }
    110                 })
    111                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
    112                     public void onClick(DialogInterface dialog, int whichButton) {
    113 
    114                         /* User clicked Cancel so do some stuff */
    115                     }
    116                 })
    117                 .create();
    118         case DIALOG_LIST:
    119             return new AlertDialog.Builder(AlertDialogSamples.this)
    120                 .setTitle(R.string.select_dialog)
    121                 .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
    122                     public void onClick(DialogInterface dialog, int which) {
    123 
    124                         /* User clicked so do some stuff */
    125                         String[] items = getResources().getStringArray(R.array.select_dialog_items);
    126                         new AlertDialog.Builder(AlertDialogSamples.this)
    127                                 .setMessage("You selected: " + which + " , " + items[which])
    128                                 .show();
    129                     }
    130                 })
    131                 .create();
    132         case DIALOG_PROGRESS:
    133             mProgressDialog = new ProgressDialog(AlertDialogSamples.this);
    134             mProgressDialog.setIcon(R.drawable.alert_dialog_icon);
    135             mProgressDialog.setTitle(R.string.select_dialog);
    136             mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    137             mProgressDialog.setMax(MAX_PROGRESS);
    138             mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE,
    139                     getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() {
    140                 public void onClick(DialogInterface dialog, int whichButton) {
    141 
    142                     /* User clicked Yes so do some stuff */
    143                 }
    144             });
    145             mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
    146                     getText(R.string.alert_dialog_cancel), new DialogInterface.OnClickListener() {
    147                 public void onClick(DialogInterface dialog, int whichButton) {
    148 
    149                     /* User clicked No so do some stuff */
    150                 }
    151             });
    152             return mProgressDialog;
    153         case DIALOG_SINGLE_CHOICE:
    154             return new AlertDialog.Builder(AlertDialogSamples.this)
    155                 .setIcon(R.drawable.alert_dialog_icon)
    156                 .setTitle(R.string.alert_dialog_single_choice)
    157                 .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
    158                     public void onClick(DialogInterface dialog, int whichButton) {
    159 
    160                         /* User clicked on a radio button do some stuff */
    161                     }
    162                 })
    163                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
    164                     public void onClick(DialogInterface dialog, int whichButton) {
    165 
    166                         /* User clicked Yes so do some stuff */
    167                     }
    168                 })
    169                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
    170                     public void onClick(DialogInterface dialog, int whichButton) {
    171 
    172                         /* User clicked No so do some stuff */
    173                     }
    174                 })
    175                .create();
    176         case DIALOG_MULTIPLE_CHOICE:
    177             return new AlertDialog.Builder(AlertDialogSamples.this)
    178                 .setIcon(R.drawable.ic_popup_reminder)
    179                 .setTitle(R.string.alert_dialog_multi_choice)
    180                 .setMultiChoiceItems(R.array.select_dialog_items3,
    181                         new boolean[]{false, true, false, true, false, false, false},
    182                         new DialogInterface.OnMultiChoiceClickListener() {
    183                             public void onClick(DialogInterface dialog, int whichButton,
    184                                     boolean isChecked) {
    185 
    186                                 /* User clicked on a check box do some stuff */
    187                             }
    188                         })
    189                 .setPositiveButton(R.string.alert_dialog_ok,
    190                         new DialogInterface.OnClickListener() {
    191                     public void onClick(DialogInterface dialog, int whichButton) {
    192 
    193                         /* User clicked Yes so do some stuff */
    194                     }
    195                 })
    196                 .setNegativeButton(R.string.alert_dialog_cancel,
    197                         new DialogInterface.OnClickListener() {
    198                     public void onClick(DialogInterface dialog, int whichButton) {
    199 
    200                         /* User clicked No so do some stuff */
    201                     }
    202                 })
    203                .create();
    204             case DIALOG_MULTIPLE_CHOICE_CURSOR:
    205                 String[] projection = new String[] {
    206                         ContactsContract.Contacts._ID,
    207                         ContactsContract.Contacts.DISPLAY_NAME,
    208                         ContactsContract.Contacts.SEND_TO_VOICEMAIL
    209                 };
    210                 Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
    211                         projection, null, null, null);
    212                 return new AlertDialog.Builder(AlertDialogSamples.this)
    213                     .setIcon(R.drawable.ic_popup_reminder)
    214                     .setTitle(R.string.alert_dialog_multi_choice_cursor)
    215                     .setMultiChoiceItems(cursor,
    216                             ContactsContract.Contacts.SEND_TO_VOICEMAIL,
    217                             ContactsContract.Contacts.DISPLAY_NAME,
    218                             new DialogInterface.OnMultiChoiceClickListener() {
    219                                 public void onClick(DialogInterface dialog, int whichButton,
    220                                         boolean isChecked) {
    221                                     Toast.makeText(AlertDialogSamples.this,
    222                                             "Readonly Demo Only - Data will not be updated",
    223                                             Toast.LENGTH_SHORT).show();
    224                                 }
    225                             })
    226                    .create();
    227         case DIALOG_TEXT_ENTRY:
    228             // This example shows how to add a custom layout to an AlertDialog
    229             LayoutInflater factory = LayoutInflater.from(this);
    230             final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
    231             return new AlertDialog.Builder(AlertDialogSamples.this)
    232                 .setIcon(R.drawable.alert_dialog_icon)
    233                 .setTitle(R.string.alert_dialog_text_entry)
    234                 .setView(textEntryView)
    235                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
    236                     public void onClick(DialogInterface dialog, int whichButton) {
    237 
    238                         /* User clicked OK so do some stuff */
    239                     }
    240                 })
    241                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
    242                     public void onClick(DialogInterface dialog, int whichButton) {
    243 
    244                         /* User clicked cancel so do some stuff */
    245                     }
    246                 })
    247                 .create();
    248         }
    249         return null;
    250     }
    251 
    252     /**
    253      * Initialization of the Activity after it is first created.  Must at least
    254      * call {@link android.app.Activity#setContentView(int)} to
    255      * describe what is to be displayed in the screen.
    256      */
    257     @Override
    258 	protected void onCreate(Bundle savedInstanceState) {
    259         super.onCreate(savedInstanceState);
    260 
    261         setContentView(R.layout.alert_dialog);
    262 
    263         /* Display a text message with yes/no buttons and handle each message as well as the cancel action */
    264         Button twoButtonsTitle = (Button) findViewById(R.id.two_buttons);
    265         twoButtonsTitle.setOnClickListener(new OnClickListener() {
    266             public void onClick(View v) {
    267                 showDialog(DIALOG_YES_NO_MESSAGE);
    268             }
    269         });
    270 
    271         /* Display a long text message with yes/no buttons and handle each message as well as the cancel action */
    272         Button twoButtons2Title = (Button) findViewById(R.id.two_buttons2);
    273         twoButtons2Title.setOnClickListener(new OnClickListener() {
    274             public void onClick(View v) {
    275                 showDialog(DIALOG_YES_NO_LONG_MESSAGE);
    276             }
    277         });
    278 
    279 
    280         /* Display a list of items */
    281         Button selectButton = (Button) findViewById(R.id.select_button);
    282         selectButton.setOnClickListener(new OnClickListener() {
    283             public void onClick(View v) {
    284                 showDialog(DIALOG_LIST);
    285             }
    286         });
    287 
    288         /* Display a custom progress bar */
    289         Button progressButton = (Button) findViewById(R.id.progress_button);
    290         progressButton.setOnClickListener(new OnClickListener() {
    291             public void onClick(View v) {
    292                 showDialog(DIALOG_PROGRESS);
    293                 mProgress = 0;
    294                 mProgressDialog.setProgress(0);
    295                 mProgressHandler.sendEmptyMessage(0);
    296             }
    297         });
    298 
    299         /* Display a radio button group */
    300         Button radioButton = (Button) findViewById(R.id.radio_button);
    301         radioButton.setOnClickListener(new OnClickListener() {
    302             public void onClick(View v) {
    303                 showDialog(DIALOG_SINGLE_CHOICE);
    304             }
    305         });
    306 
    307         /* Display a list of checkboxes */
    308         Button checkBox = (Button) findViewById(R.id.checkbox_button);
    309         checkBox.setOnClickListener(new OnClickListener() {
    310             public void onClick(View v) {
    311                 showDialog(DIALOG_MULTIPLE_CHOICE);
    312             }
    313         });
    314 
    315         /* Display a list of checkboxes, backed by a cursor */
    316         Button checkBox2 = (Button) findViewById(R.id.checkbox_button2);
    317         checkBox2.setOnClickListener(new OnClickListener() {
    318             public void onClick(View v) {
    319                 showDialog(DIALOG_MULTIPLE_CHOICE_CURSOR);
    320             }
    321         });
    322 
    323         /* Display a text entry dialog */
    324         Button textEntry = (Button) findViewById(R.id.text_entry_button);
    325         textEntry.setOnClickListener(new OnClickListener() {
    326             public void onClick(View v) {
    327                 showDialog(DIALOG_TEXT_ENTRY);
    328             }
    329         });
    330 
    331         mProgressHandler = new Handler() {
    332             @Override
    333             public void handleMessage(Message msg) {
    334                 super.handleMessage(msg);
    335                 if (mProgress >= MAX_PROGRESS) {
    336                     mProgressDialog.dismiss();
    337                 } else {
    338                     mProgress++;
    339                     mProgressDialog.incrementProgressBy(1);
    340                     mProgressHandler.sendEmptyMessageDelayed(0, 100);
    341                 }
    342             }
    343         };
    344     }
    345 }
    346