Handling User Input android application development for for dummies

247

Chapter 11: Handling User Input

Creating the showDialog method The showDialog method performs some work for you in the base activity class, and at the end of the day, the only thing you need to know is that by calling showDialog with an ID, the activity’s onCreateDialog method is called. At the bottom of your class file, type the code from Listing 11-3 to respond to the showDialog method call. Listing 11-3: Responding to showDialog with onCreateDialog Override protected Dialog onCreateDialogint id { ➝ 2 switchid { case DATE_PICKER_DIALOG: ➝ 4 return showDatePicker; } return super.onCreateDialogid; } private DatePickerDialog showDatePicker { ➝ 10 DatePickerDialog datePicker = new DatePickerDialogReminderEditActivity.this, new DatePickerDialog.OnDateSetListener { ➝ 13 Override public void onDateSetDatePicker view, int year, int monthOfYear, int dayOfMonth { ➝ 17 mCalendar.setCalendar.YEAR, year; ➝ 19 mCalendar.setCalendar.MONTH, monthOfYear; mCalendar.setCalendar.DAY_OF_MONTH, dayOfMonth; ➝ 21 updateDateButtonText; ➝ 22 } }, mCalendar.getCalendar.YEAR, mCalendar.getCalendar.MONTH, mCalendar.getCalendar.DAY_OF_MONTH; ➝ 25 return datePicker; ➝ 26 } private void updateDateButtonText { ➝ 29 SimpleDateFormat dateFormat = new SimpleDateFormatDATE_FORMAT; ➝ 30 String dateForButton = dateFormat.formatmCalendar.getTime; ➝ 31 mDateButton.setTextdateForButton; ➝ 32 } Each important line of code is explained as follows: ➝ 2 The onCreateDialog method is overridden and called when the showDialog method is called with a parameter. The int id parameter is the ID that was passed into the showDialog method previously. 248 Part III: Creating a Feature-Rich Application ➝ 4 This line of code determines whether the ID passed into the onCreateDialog is the same one that was passed in as a parameter to the showDialog method. If it matches the DATE_PICKER_DIALOG value, it returns the value of the show- DatePicker method. The showDatePicker call must return a Dialog type for onCreateDialog to show a dialog box. ➝ 10 The showDatePicker method definition that returns a DatePickerDialog. ➝ 13 On this line, I am creating a new DatePickerDialog that accepts the current context as the first parameter. I have provided the cur- rent instance ReminderEditActivity.this as the Context. The full class name is included because it’s inside a nested state- ment, therefore fully qualified names are required. The next parameter is the onDateSetListener, which provides a callback that is defined from line 13 through line 22. This callback provides the value of the date that was chosen through the date picker. The other parameters for the DatePickerDialog are listed on line 25. ➝ 17 The implementation of the onDateSet method that is called when the user sets the date through the DatePickerDialog and clicks the Set button. This method provides the following parameters: • DatePicker view: The date picker used in the date selection dialog box • int year: The year that was set • int monthOfYear: The month that was set in format 0–11 for compatibility with the Calendar object • int dayOfMonth: The day of the month ➝ 19 through ➝ 21 This code block uses a variable by the name of mCalendar. This is a classwide Calendar variable that allows me to keep track of the date and time that the user set while inside the ReminderEditActivity through the DatePickerDialog and TimePickerDialog. You also need this variable — define a classwide Calendar variable at the top of the class file with the name of mCalendar. In this code block, I am using the setter and Calendar constants to change the date values of the Calendar object to that of the values the user set through the DatePickerDialog. private Calendar mCalendar; mCalendar = Calendar.getInstance; Inside the onCreate method, provide the mCalendar object with a value using the getInstance method. This method returns a new instance of the Calendar object. 249

Chapter 11: Handling User Input