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