Sunday, 27 November 2011

5. How to use the DatePickerDialog

Hint: study onCreateDialog method in the QuizSettingsActivity class.

6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Adding DatePickerDialog to QuizSeFngsAc/vity involves several
    steps:
    1. Define a unique identfier for the dialog within the actvity.
    2. Implement the onCreateDialog() method of the ac/vity to return DatePickerDialog when supplied the unique identfier.
    3. Implement the onPrepareDialog() method of the actvity to initalize DatePickerDialog with
    the date of birth preference or the current date.
    4. Launch the DatePickerDialog using the
    showDialog() method, with the unique Dialog identfier

    ReplyDelete
  3. Calendar now = Calendar.getInstance();
    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();
    }
    }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));

    ReplyDelete
  4. Initialise DatePickerDialog and add OnDateSetListener to implement onDateSet Method for updating the
    date value.
    Get current time and set the updated values of year,month and dayofmonth
    Convert to milliseconds and then to the required format to display.

    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();

    }
    }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Calendar now = Calendar.geiInstance();
    DatePickerDialog dateDialog = new DatePickerDialog(this, new DatePickerDialog.onDateSetListerner() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    Time dateOfBirth = new Time();
    dateOfBirth.set(dateOfMonth, monthOfYear, year);
    long dtDOB = dateOfBirth.toMills(true);
    dob.setText(DateFormat.format("MMMM dd, yyyy", dtDob));
    Editor editor = mGameSettings.edit();
    editor.putLong(GAME_PREFERENCEs_DOB, dtDOB);
    editor.commit();
    }
    }, now.get(calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH)

    ReplyDelete