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 32 import com.example.android.apis.R; 33 34 /** 35 * Example of how to use an {@link android.app.AlertDialog}. 36 * <h3>AlertDialogSamples</h3> 37 38 <p>This demonstrates the different ways the AlertDialog can be used.</p> 39 40 <h4>Demo</h4> 41 App/Dialog/Alert Dialog 42 43 <h4>Source files</h4> 44 * <table class="LinkTable"> 45 * <tr> 46 * <td >src/com.example.android.apis/app/AlertDialogSamples.java</td> 47 * <td >The Alert Dialog Samples implementation</td> 48 * </tr> 49 * <tr> 50 * <td >/res/any/layout/alert_dialog.xml</td> 51 * <td >Defines contents of the screen</td> 52 * </tr> 53 * </table> 54 */ 55 public class AlertDialogSamples extends Activity { 56 private static final int DIALOG_YES_NO_MESSAGE = 1; 57 private static final int DIALOG_YES_NO_LONG_MESSAGE = 2; 58 private static final int DIALOG_LIST = 3; 59 private static final int DIALOG_PROGRESS = 4; 60 private static final int DIALOG_SINGLE_CHOICE = 5; 61 private static final int DIALOG_MULTIPLE_CHOICE = 6; 62 private static final int DIALOG_TEXT_ENTRY = 7; 63 64 private static final int MAX_PROGRESS = 100; 65 66 private ProgressDialog mProgressDialog; 67 private int mProgress; 68 private Handler mProgressHandler; 69 70 @Override onCreateDialog(int id)71 protected Dialog onCreateDialog(int id) { 72 switch (id) { 73 case DIALOG_YES_NO_MESSAGE: 74 return new AlertDialog.Builder(AlertDialogSamples.this) 75 .setIcon(R.drawable.alert_dialog_icon) 76 .setTitle(R.string.alert_dialog_two_buttons_title) 77 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 78 public void onClick(DialogInterface dialog, int whichButton) { 79 80 /* User clicked OK so do some stuff */ 81 } 82 }) 83 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { 84 public void onClick(DialogInterface dialog, int whichButton) { 85 86 /* User clicked Cancel so do some stuff */ 87 } 88 }) 89 .create(); 90 case DIALOG_YES_NO_LONG_MESSAGE: 91 return new AlertDialog.Builder(AlertDialogSamples.this) 92 .setIcon(R.drawable.alert_dialog_icon) 93 .setTitle(R.string.alert_dialog_two_buttons_msg) 94 .setMessage(R.string.alert_dialog_two_buttons2_msg) 95 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 96 public void onClick(DialogInterface dialog, int whichButton) { 97 98 /* User clicked OK so do some stuff */ 99 } 100 }) 101 .setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() { 102 public void onClick(DialogInterface dialog, int whichButton) { 103 104 /* User clicked Something so do some stuff */ 105 } 106 }) 107 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { 108 public void onClick(DialogInterface dialog, int whichButton) { 109 110 /* User clicked Cancel so do some stuff */ 111 } 112 }) 113 .create(); 114 case DIALOG_LIST: 115 return new AlertDialog.Builder(AlertDialogSamples.this) 116 .setTitle(R.string.select_dialog) 117 .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { 118 public void onClick(DialogInterface dialog, int which) { 119 120 /* User clicked so do some stuff */ 121 String[] items = getResources().getStringArray(R.array.select_dialog_items); 122 new AlertDialog.Builder(AlertDialogSamples.this) 123 .setMessage("You selected: " + which + " , " + items[which]) 124 .show(); 125 } 126 }) 127 .create(); 128 case DIALOG_PROGRESS: 129 mProgressDialog = new ProgressDialog(AlertDialogSamples.this); 130 mProgressDialog.setIcon(R.drawable.alert_dialog_icon); 131 mProgressDialog.setTitle(R.string.select_dialog); 132 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 133 mProgressDialog.setMax(MAX_PROGRESS); 134 mProgressDialog.setButton(getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() { 135 public void onClick(DialogInterface dialog, int whichButton) { 136 137 /* User clicked Yes so do some stuff */ 138 } 139 }); 140 mProgressDialog.setButton2(getText(R.string.alert_dialog_cancel), new DialogInterface.OnClickListener() { 141 public void onClick(DialogInterface dialog, int whichButton) { 142 143 /* User clicked No so do some stuff */ 144 } 145 }); 146 return mProgressDialog; 147 case DIALOG_SINGLE_CHOICE: 148 return new AlertDialog.Builder(AlertDialogSamples.this) 149 .setIcon(R.drawable.alert_dialog_icon) 150 .setTitle(R.string.alert_dialog_single_choice) 151 .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() { 152 public void onClick(DialogInterface dialog, int whichButton) { 153 154 /* User clicked on a radio button do some stuff */ 155 } 156 }) 157 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 158 public void onClick(DialogInterface dialog, int whichButton) { 159 160 /* User clicked Yes so do some stuff */ 161 } 162 }) 163 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { 164 public void onClick(DialogInterface dialog, int whichButton) { 165 166 /* User clicked No so do some stuff */ 167 } 168 }) 169 .create(); 170 case DIALOG_MULTIPLE_CHOICE: 171 return new AlertDialog.Builder(AlertDialogSamples.this) 172 .setIcon(R.drawable.ic_popup_reminder) 173 .setTitle(R.string.alert_dialog_multi_choice) 174 .setMultiChoiceItems(R.array.select_dialog_items3, 175 new boolean[]{false, true, false, true, false, false, false}, 176 new DialogInterface.OnMultiChoiceClickListener() { 177 public void onClick(DialogInterface dialog, int whichButton, 178 boolean isChecked) { 179 180 /* User clicked on a check box do some stuff */ 181 } 182 }) 183 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 184 public void onClick(DialogInterface dialog, int whichButton) { 185 186 /* User clicked Yes 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 No so do some stuff */ 193 } 194 }) 195 .create(); 196 case DIALOG_TEXT_ENTRY: 197 // This example shows how to add a custom layout to an AlertDialog 198 LayoutInflater factory = LayoutInflater.from(this); 199 final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null); 200 return new AlertDialog.Builder(AlertDialogSamples.this) 201 .setIcon(R.drawable.alert_dialog_icon) 202 .setTitle(R.string.alert_dialog_text_entry) 203 .setView(textEntryView) 204 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 205 public void onClick(DialogInterface dialog, int whichButton) { 206 207 /* User clicked OK so do some stuff */ 208 } 209 }) 210 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { 211 public void onClick(DialogInterface dialog, int whichButton) { 212 213 /* User clicked cancel so do some stuff */ 214 } 215 }) 216 .create(); 217 } 218 return null; 219 } 220 221 /** 222 * Initialization of the Activity after it is first created. Must at least 223 * call {@link android.app.Activity#setContentView(int)} to 224 * describe what is to be displayed in the screen. 225 */ 226 @Override 227 protected void onCreate(Bundle savedInstanceState) { 228 super.onCreate(savedInstanceState); 229 230 setContentView(R.layout.alert_dialog); 231 232 /* Display a text message with yes/no buttons and handle each message as well as the cancel action */ 233 Button twoButtonsTitle = (Button) findViewById(R.id.two_buttons); 234 twoButtonsTitle.setOnClickListener(new OnClickListener() { 235 public void onClick(View v) { 236 showDialog(DIALOG_YES_NO_MESSAGE); 237 } 238 }); 239 240 /* Display a long text message with yes/no buttons and handle each message as well as the cancel action */ 241 Button twoButtons2Title = (Button) findViewById(R.id.two_buttons2); 242 twoButtons2Title.setOnClickListener(new OnClickListener() { 243 public void onClick(View v) { 244 showDialog(DIALOG_YES_NO_LONG_MESSAGE); 245 } 246 }); 247 248 249 /* Display a list of items */ 250 Button selectButton = (Button) findViewById(R.id.select_button); 251 selectButton.setOnClickListener(new OnClickListener() { 252 public void onClick(View v) { 253 showDialog(DIALOG_LIST); 254 } 255 }); 256 257 /* Display a custom progress bar */ 258 Button progressButton = (Button) findViewById(R.id.progress_button); 259 progressButton.setOnClickListener(new OnClickListener() { 260 public void onClick(View v) { 261 showDialog(DIALOG_PROGRESS); 262 mProgress = 0; 263 mProgressDialog.setProgress(0); 264 mProgressHandler.sendEmptyMessage(0); 265 } 266 }); 267 268 /* Display a radio button group */ 269 Button radioButton = (Button) findViewById(R.id.radio_button); 270 radioButton.setOnClickListener(new OnClickListener() { 271 public void onClick(View v) { 272 showDialog(DIALOG_SINGLE_CHOICE); 273 } 274 }); 275 276 /* Display a list of checkboxes */ 277 Button checkBox = (Button) findViewById(R.id.checkbox_button); 278 checkBox.setOnClickListener(new OnClickListener() { 279 public void onClick(View v) { 280 showDialog(DIALOG_MULTIPLE_CHOICE); 281 } 282 }); 283 284 /* Display a text entry dialog */ 285 Button textEntry = (Button) findViewById(R.id.text_entry_button); 286 textEntry.setOnClickListener(new OnClickListener() { 287 public void onClick(View v) { 288 showDialog(DIALOG_TEXT_ENTRY); 289 } 290 }); 291 292 mProgressHandler = new Handler() { 293 @Override 294 public void handleMessage(Message msg) { 295 super.handleMessage(msg); 296 if (mProgress >= MAX_PROGRESS) { 297 mProgressDialog.dismiss(); 298 } else { 299 mProgress++; 300 mProgressDialog.incrementProgressBy(1); 301 mProgressHandler.sendEmptyMessageDelayed(0, 100); 302 } 303 } 304 }; 305 } 306 } 307