Question

I have a TextView on my Activity which is used to display a date. When the user clicks on the TextView I launch a DatePickerDialog like so:

public void onClick(View v) {
    if (v.getId() == R.id.date_wrapper) {
        showDialog(DATE_DIALOG_ID);
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
       GregorianCalendar date = new GregorianCalendar();

       if (mData != null) {
           date.setTimeInMillis(mData.getDate());
       }

       return new DatePickerDialog(this, datePickerListener, date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
    }

    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {

        GregorianCalendar selectedDate = new GregorianCalendar();
        selectedDate.set(Calendar.YEAR, selectedYear);
        selectedDate.set(Calendar.MONTH, selectedMonth);
        selectedDate.set(Calendar.DAY_OF_MONTH, selectedDay);

        mData.setDate(selectedDate.getTimeInMillis());

        populateDate();
    }
};

This works great. However, when a user clicks on a submit button, I want to set the date back to today. I can easily set the mData object's Date variable to today. However, I'm not sure how to update the DatePickerDialog. It is already created, so clicking on the TextView does not run onCreateDialog again. So, when I click on the TextView, the DatePickerDialog opens and it is the last date I chose.

How do I reference the DatePickerDialog to update the date? Killing the DatePickerDialog would also be acceptable.

Was it helpful?

Solution

I have found an acceptable solution.

When I reset the date, I should also call removeDialog(DATE_DIALOG_ID); to destroy the Dialog. The next time showDialog(DATE_DIALOG_ID); is called, it will be recreated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top