• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     private static final int DIALOG_YES_NO_ULTRA_LONG_MESSAGE = 9;
68     private static final int DIALOG_YES_NO_OLD_SCHOOL_MESSAGE = 10;
69     private static final int DIALOG_YES_NO_HOLO_LIGHT_MESSAGE = 11;
70 
71     private static final int MAX_PROGRESS = 100;
72 
73     private ProgressDialog mProgressDialog;
74     private int mProgress;
75     private Handler mProgressHandler;
76 
77     @Override
onCreateDialog(int id)78     protected Dialog onCreateDialog(int id) {
79         switch (id) {
80         case DIALOG_YES_NO_MESSAGE:
81             return new AlertDialog.Builder(AlertDialogSamples.this)
82                 .setIconAttribute(android.R.attr.alertDialogIcon)
83                 .setTitle(R.string.alert_dialog_two_buttons_title)
84                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
85                     public void onClick(DialogInterface dialog, int whichButton) {
86 
87                         /* User clicked OK so do some stuff */
88                     }
89                 })
90                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
91                     public void onClick(DialogInterface dialog, int whichButton) {
92 
93                         /* User clicked Cancel so do some stuff */
94                     }
95                 })
96                 .create();
97         case DIALOG_YES_NO_OLD_SCHOOL_MESSAGE:
98             return new AlertDialog.Builder(AlertDialogSamples.this, AlertDialog.THEME_TRADITIONAL)
99                 .setIconAttribute(android.R.attr.alertDialogIcon)
100                 .setTitle(R.string.alert_dialog_two_buttons_title)
101                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
102                     public void onClick(DialogInterface dialog, int whichButton) {
103                     }
104                 })
105                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
106                     public void onClick(DialogInterface dialog, int whichButton) {
107                     }
108                 })
109                 .create();
110         case DIALOG_YES_NO_HOLO_LIGHT_MESSAGE:
111             return new AlertDialog.Builder(AlertDialogSamples.this, AlertDialog.THEME_HOLO_LIGHT)
112                 .setIconAttribute(android.R.attr.alertDialogIcon)
113                 .setTitle(R.string.alert_dialog_two_buttons_title)
114                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
115                     public void onClick(DialogInterface dialog, int whichButton) {
116                     }
117                 })
118                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
119                     public void onClick(DialogInterface dialog, int whichButton) {
120                     }
121                 })
122                 .create();
123         case DIALOG_YES_NO_LONG_MESSAGE:
124             return new AlertDialog.Builder(AlertDialogSamples.this)
125                 .setIconAttribute(android.R.attr.alertDialogIcon)
126                 .setTitle(R.string.alert_dialog_two_buttons_msg)
127                 .setMessage(R.string.alert_dialog_two_buttons2_msg)
128                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
129                     public void onClick(DialogInterface dialog, int whichButton) {
130 
131                         /* User clicked OK so do some stuff */
132                     }
133                 })
134                 .setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {
135                     public void onClick(DialogInterface dialog, int whichButton) {
136 
137                         /* User clicked Something so do some stuff */
138                     }
139                 })
140                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
141                     public void onClick(DialogInterface dialog, int whichButton) {
142 
143                         /* User clicked Cancel so do some stuff */
144                     }
145                 })
146                 .create();
147         case DIALOG_YES_NO_ULTRA_LONG_MESSAGE:
148             return new AlertDialog.Builder(AlertDialogSamples.this)
149                 .setIconAttribute(android.R.attr.alertDialogIcon)
150                 .setTitle(R.string.alert_dialog_two_buttons_msg)
151                 .setMessage(R.string.alert_dialog_two_buttons2ultra_msg)
152                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
153                     public void onClick(DialogInterface dialog, int whichButton) {
154 
155                         /* User clicked OK so do some stuff */
156                     }
157                 })
158                 .setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {
159                     public void onClick(DialogInterface dialog, int whichButton) {
160 
161                         /* User clicked Something so do some stuff */
162                     }
163                 })
164                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
165                     public void onClick(DialogInterface dialog, int whichButton) {
166 
167                         /* User clicked Cancel so do some stuff */
168                     }
169                 })
170                 .create();
171         case DIALOG_LIST:
172             return new AlertDialog.Builder(AlertDialogSamples.this)
173                 .setTitle(R.string.select_dialog)
174                 .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
175                     public void onClick(DialogInterface dialog, int which) {
176 
177                         /* User clicked so do some stuff */
178                         String[] items = getResources().getStringArray(R.array.select_dialog_items);
179                         new AlertDialog.Builder(AlertDialogSamples.this)
180                                 .setMessage("You selected: " + which + " , " + items[which])
181                                 .show();
182                     }
183                 })
184                 .create();
185         case DIALOG_PROGRESS:
186             mProgressDialog = new ProgressDialog(AlertDialogSamples.this);
187             mProgressDialog.setIconAttribute(android.R.attr.alertDialogIcon);
188             mProgressDialog.setTitle(R.string.select_dialog);
189             mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
190             mProgressDialog.setMax(MAX_PROGRESS);
191             mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE,
192                     getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() {
193                 public void onClick(DialogInterface dialog, int whichButton) {
194 
195                     /* User clicked Yes so do some stuff */
196                 }
197             });
198             mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
199                     getText(R.string.alert_dialog_cancel), new DialogInterface.OnClickListener() {
200                 public void onClick(DialogInterface dialog, int whichButton) {
201 
202                     /* User clicked No so do some stuff */
203                 }
204             });
205             return mProgressDialog;
206         case DIALOG_SINGLE_CHOICE:
207             return new AlertDialog.Builder(AlertDialogSamples.this)
208                 .setIconAttribute(android.R.attr.alertDialogIcon)
209                 .setTitle(R.string.alert_dialog_single_choice)
210                 .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
211                     public void onClick(DialogInterface dialog, int whichButton) {
212 
213                         /* User clicked on a radio button do some stuff */
214                     }
215                 })
216                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
217                     public void onClick(DialogInterface dialog, int whichButton) {
218 
219                         /* User clicked Yes so do some stuff */
220                     }
221                 })
222                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
223                     public void onClick(DialogInterface dialog, int whichButton) {
224 
225                         /* User clicked No so do some stuff */
226                     }
227                 })
228                .create();
229         case DIALOG_MULTIPLE_CHOICE:
230             return new AlertDialog.Builder(AlertDialogSamples.this)
231                 .setIcon(R.drawable.ic_popup_reminder)
232                 .setTitle(R.string.alert_dialog_multi_choice)
233                 .setMultiChoiceItems(R.array.select_dialog_items3,
234                         new boolean[]{false, true, false, true, false, false, false},
235                         new DialogInterface.OnMultiChoiceClickListener() {
236                             public void onClick(DialogInterface dialog, int whichButton,
237                                     boolean isChecked) {
238 
239                                 /* User clicked on a check box do some stuff */
240                             }
241                         })
242                 .setPositiveButton(R.string.alert_dialog_ok,
243                         new DialogInterface.OnClickListener() {
244                     public void onClick(DialogInterface dialog, int whichButton) {
245 
246                         /* User clicked Yes so do some stuff */
247                     }
248                 })
249                 .setNegativeButton(R.string.alert_dialog_cancel,
250                         new DialogInterface.OnClickListener() {
251                     public void onClick(DialogInterface dialog, int whichButton) {
252 
253                         /* User clicked No so do some stuff */
254                     }
255                 })
256                .create();
257             case DIALOG_MULTIPLE_CHOICE_CURSOR:
258                 String[] projection = new String[] {
259                         ContactsContract.Contacts._ID,
260                         ContactsContract.Contacts.DISPLAY_NAME,
261                         ContactsContract.Contacts.SEND_TO_VOICEMAIL
262                 };
263                 Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
264                         projection, null, null, null);
265                 return new AlertDialog.Builder(AlertDialogSamples.this)
266                     .setIcon(R.drawable.ic_popup_reminder)
267                     .setTitle(R.string.alert_dialog_multi_choice_cursor)
268                     .setMultiChoiceItems(cursor,
269                             ContactsContract.Contacts.SEND_TO_VOICEMAIL,
270                             ContactsContract.Contacts.DISPLAY_NAME,
271                             new DialogInterface.OnMultiChoiceClickListener() {
272                                 public void onClick(DialogInterface dialog, int whichButton,
273                                         boolean isChecked) {
274                                     Toast.makeText(AlertDialogSamples.this,
275                                             "Readonly Demo Only - Data will not be updated",
276                                             Toast.LENGTH_SHORT).show();
277                                 }
278                             })
279                    .create();
280         case DIALOG_TEXT_ENTRY:
281             // This example shows how to add a custom layout to an AlertDialog
282             LayoutInflater factory = LayoutInflater.from(this);
283             final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
284             return new AlertDialog.Builder(AlertDialogSamples.this)
285                 .setIconAttribute(android.R.attr.alertDialogIcon)
286                 .setTitle(R.string.alert_dialog_text_entry)
287                 .setView(textEntryView)
288                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
289                     public void onClick(DialogInterface dialog, int whichButton) {
290 
291                         /* User clicked OK so do some stuff */
292                     }
293                 })
294                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
295                     public void onClick(DialogInterface dialog, int whichButton) {
296 
297                         /* User clicked cancel so do some stuff */
298                     }
299                 })
300                 .create();
301         }
302         return null;
303     }
304 
305     /**
306      * Initialization of the Activity after it is first created.  Must at least
307      * call {@link android.app.Activity#setContentView(int)} to
308      * describe what is to be displayed in the screen.
309      */
310     @Override
311 	protected void onCreate(Bundle savedInstanceState) {
312         super.onCreate(savedInstanceState);
313 
314         setContentView(R.layout.alert_dialog);
315 
316         /* Display a text message with yes/no buttons and handle each message as well as the cancel action */
317         Button twoButtonsTitle = (Button) findViewById(R.id.two_buttons);
318         twoButtonsTitle.setOnClickListener(new OnClickListener() {
319             public void onClick(View v) {
320                 showDialog(DIALOG_YES_NO_MESSAGE);
321             }
322         });
323 
324         /* Display a long text message with yes/no buttons and handle each message as well as the cancel action */
325         Button twoButtons2Title = (Button) findViewById(R.id.two_buttons2);
326         twoButtons2Title.setOnClickListener(new OnClickListener() {
327             public void onClick(View v) {
328                 showDialog(DIALOG_YES_NO_LONG_MESSAGE);
329             }
330         });
331 
332 
333         /* Display an ultra long text message with yes/no buttons and handle each message as well as the cancel action */
334         Button twoButtons2UltraTitle = (Button) findViewById(R.id.two_buttons2ultra);
335         twoButtons2UltraTitle.setOnClickListener(new OnClickListener() {
336             public void onClick(View v) {
337                 showDialog(DIALOG_YES_NO_ULTRA_LONG_MESSAGE);
338             }
339         });
340 
341 
342         /* Display a list of items */
343         Button selectButton = (Button) findViewById(R.id.select_button);
344         selectButton.setOnClickListener(new OnClickListener() {
345             public void onClick(View v) {
346                 showDialog(DIALOG_LIST);
347             }
348         });
349 
350         /* Display a custom progress bar */
351         Button progressButton = (Button) findViewById(R.id.progress_button);
352         progressButton.setOnClickListener(new OnClickListener() {
353             public void onClick(View v) {
354                 showDialog(DIALOG_PROGRESS);
355                 mProgress = 0;
356                 mProgressDialog.setProgress(0);
357                 mProgressHandler.sendEmptyMessage(0);
358             }
359         });
360 
361         /* Display a radio button group */
362         Button radioButton = (Button) findViewById(R.id.radio_button);
363         radioButton.setOnClickListener(new OnClickListener() {
364             public void onClick(View v) {
365                 showDialog(DIALOG_SINGLE_CHOICE);
366             }
367         });
368 
369         /* Display a list of checkboxes */
370         Button checkBox = (Button) findViewById(R.id.checkbox_button);
371         checkBox.setOnClickListener(new OnClickListener() {
372             public void onClick(View v) {
373                 showDialog(DIALOG_MULTIPLE_CHOICE);
374             }
375         });
376 
377         /* Display a list of checkboxes, backed by a cursor */
378         Button checkBox2 = (Button) findViewById(R.id.checkbox_button2);
379         checkBox2.setOnClickListener(new OnClickListener() {
380             public void onClick(View v) {
381                 showDialog(DIALOG_MULTIPLE_CHOICE_CURSOR);
382             }
383         });
384 
385         /* Display a text entry dialog */
386         Button textEntry = (Button) findViewById(R.id.text_entry_button);
387         textEntry.setOnClickListener(new OnClickListener() {
388             public void onClick(View v) {
389                 showDialog(DIALOG_TEXT_ENTRY);
390             }
391         });
392 
393         /* Two points, in the traditional theme */
394         Button twoButtonsOldSchoolTitle = (Button) findViewById(R.id.two_buttons_old_school);
395         twoButtonsOldSchoolTitle.setOnClickListener(new OnClickListener() {
396             public void onClick(View v) {
397                 showDialog(DIALOG_YES_NO_OLD_SCHOOL_MESSAGE);
398             }
399         });
400 
401         /* Two points, in the light holographic theme */
402         Button twoButtonsHoloLightTitle = (Button) findViewById(R.id.two_buttons_holo_light);
403         twoButtonsHoloLightTitle.setOnClickListener(new OnClickListener() {
404             public void onClick(View v) {
405                 showDialog(DIALOG_YES_NO_HOLO_LIGHT_MESSAGE);
406             }
407         });
408 
409         mProgressHandler = new Handler() {
410             @Override
411             public void handleMessage(Message msg) {
412                 super.handleMessage(msg);
413                 if (mProgress >= MAX_PROGRESS) {
414                     mProgressDialog.dismiss();
415                 } else {
416                     mProgress++;
417                     mProgressDialog.incrementProgressBy(1);
418                     mProgressHandler.sendEmptyMessageDelayed(0, 100);
419                 }
420             }
421         };
422     }
423 }
424