Removing a Dialog from

Dismissing a Dialog window does not destroy it. If the dialog is shown again, its cached contents will be redisplayed. If you want to force an activity to remove a dialog and not reuse it, you call the removeDialog() method and pass in the valid Dialog identifier.

Using DatePickerDialog

Now you should add a Dialog window to the QuizSettingsActivity class. Specifically, you should add DatePickerDialog to handle input of the user's date of birth. Adding DatePickerDialog to QuizSettingsActivity involves several steps:

1. Define a unique identifier for the dialog within the activity.

2. Implement the onCreateDialog() method of the activity to return DatePickerDialog when supplied the unique identifier.

3. Implement the onPrepareDialog() method of the activity to initialize DatePickerDialog with the date of birth preference or the current date.

4. Launch the DatePickerDialog using the showDialog() method, with the unique Dialog identifier.

Adding the DatePickerDialog to the QuizSettingsActivity Class

To create DatePickerDialog, you must first declare a unique identifier to represent the dialog, as follows:

static final int DATE_DIALOG_ID = 0;

Next, you need to implement the onCreateDialog() method of QuizSettingsActivity and include a case statement for the new Dialog identifier: ©Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID:

// TODO: Return a DatePickerDialog here

return null;

Now let's look at how to construct a DatePickerDialog in detail. Within the switch statement for DATE_DIALOG_ID, you must return a valid DatePickerDialog for display. The constructor for DatePickerDialog includes a

DatePickerDialog.OnDateSetListener parameter, where you can provide an implementation of the onDateSet() method to handle when the user chooses a specific date of birth and save it to the SharedPreferences:

DatePickerDialog dateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Time dateOfBirth = new Time(); dateOfBirth.set(dayOfMonth, monthOfYear, year); long dtDob = dateOfBirth.toMillis(true); dob.setText(DateFormat

.format("MMMM dd, yyyy", dtDob)); Editor editor = mGameSettings.edit(); editor.putLong(GAME_PREFERENCES_DOB, dtDob); editor.commit();

A DatePicker control has three different input controls: month, day, and year. Therefore, to create DatePickerDialog, you must set these date fields individually. Because DatePickerDialog can be launched any number of times, you do not initialize its date within the onCreateDialog() method but instead pass in default values (three zeros). Finally, you return the new DatePickerDialog you created in the onCreateDialog() method switch statement: return dateDialog;

Initializing DatePickerDialog

You want to initialize DatePickerDialog each and every time it is displayed, so you override the onPrepareDialog() method to set DatePicker to either today's date or the birth date saved in the game preferences.

Did you Know?

You can use the Calendar class to get the current date on the device. The Calendar class has fields for each of the "parts" of the date: day, month, and year. You can use this feature of the Calendar class to configure DatePickerDialog with a specific date.

The onPrepareDialog() method passes in both the Dialog identifier and the instance of the Dialog window, so you can modify it as needed. For example, in this case, you want to update the date of DatePickerDialog, so you use the updateDate() method: @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); switch (id) { case DATE_DIALOG_ID:

// Handle any DatePickerDialog initialization here DatePickerDialog dateDialog = (DatePickerDialog) dialog; int iDay, iMonth, iYear; // Check for date of birth preference if (mGameSettings.contains(GAME_PREFERENCES_DOB)) { // Retrieve Birth date setting from preferences long msBirthDate = mGameSettings,getLong(GAME_PREFERENCES_DOB, 0); Time dateOfBirth = new Time(); dateOfBirth.set(msBirthDate); iDay = dateOfBirth.monthDay; iMonth = dateOfBirth.month; iYear = dateOfBirth.year; } else {

Calendar cal = Calendar.getInstance(); // Today's date fields iDay = cal.get(Calendar. DAY_OF_MONTH); iMonth = cal.get(Calendar.MONTH); iYear = cal.get(Calendar. YEAR);

// Set the date in the DatePicker to the date of birth OR to the // current date dateDialog.updateDate(iYear, iMonth, iDay); return;

Launching DatePickerDialog

You have configured DatePickerDialog, but it doesn't display unless the user clicks the appropriate Button control on the main settings screen. The user triggers DatePickerDialog by pressing the Button control called Button_DOB.

You registered for click events on the Button_DOB control and currently make a Toast call. Now you can change this to call the showDialog() method, which launches DatePickerDialog, as shown in Figure 11.2:

Button pickDate = (Button) findViewById(; pickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DATE_DIALOG_ID);

| asie 8:25 PM |

Been There, Done That!

ft Settings «




^^ Thursday, January 21, 1988





ta n



| *

DatePicker-Dialog used for date of birth input.

You can use the DateFormat class to format a date string. The DateFormat class works well with Calendar dates as well as long format dates (milliseconds since epoch). For example, to format a long date in the form January 1, 2010, you could use the format() method of the DateFormat method as follows: String strDate = DateFormat.format("MMMM dd, yyyy", dtDob);

Did you Know?

0 0

Post a comment