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