Android Date Picker Dialog Weirdness

Recently I needed to add a date picker dialog to a form I was designing. It was pretty straight forward, an EditText with an adjacent button to trigger a DatePickerDialog to quickly select a date.

The following is an example from my project. I create a managed date picker dialog that is set to the date in the EditText. If the EditText is empty or has an invalid date string, the date picker dialog is set to today’s date.

Code snippet from my activity:

...

@Override
protected Dialog onCreateDialog(int id) {
	Log.d(TAG, "onCreateDialog() -> id = " + id);

	if (id == ID_DIALOG_DATE_PICKER) {
		return new DatePickerDialog(this, searchDateSetListener, 0, 0, 0);
	}
		
	return super.onCreateDialog(id);
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
	Log.d(TAG, "onPrepareDialog() -> id = " + id);
	
	if (id == ID_DIALOG_DATE_PICKER) {
		DatePickerDialog dlg = (DatePickerDialog) dialog;
		
		// Get today's date
		Calendar c = Calendar.getInstance();
		
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);
		
		// Get the search filter
		String filter = editSearchFrom.getText().toString().trim();
			
		// Update the date picker dialog
		try {
			Date date = dateFormat.parse(filter);
			c.setTime(date);
			dlg.updateDate(
				c.get(Calendar.YEAR),
				c.get(Calendar.MONTH),
				c.get(Calendar.DAY_OF_MONTH));
		} catch (ParseException e) {
			dlg.updateDate(year, month, day);
		}
	}
}

...


This works as expected except for one minor detail. The date picker dialog title is not updated with the correct date as shown below:

The updateDate() method apparently updates the picker selection widgets without reflecting those changes in the dialog’s title. A bit of research on the web pointed me to the following issue on Google Code. It appears that others have encountered the same problem and are looking for a resolution.

There is, however, a simple workaround to the problem. The first step is to create a new date formatter that will display the date in the same format as the dialog:

DateFormat longDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy");

In my project, the actual format string is saved as a string resource and the formatter object is a class level member variable. The formatter is initialized in the activity’s onCreate() method. The final step is to use the formatter in conjunction with the date picker dialog’s setTitle() method to update the title with the correct date:

...
// Update the date picker dialog
try {
	Date date = dateFormat.parse(filter);
	c.setTime(date);
	dlg.setTitle(longDateFormat.format(date));		// New -- update dialog title
	dlg.updateDate(
			c.get(Calendar.YEAR),
			c.get(Calendar.MONTH),
			c.get(Calendar.DAY_OF_MONTH));
} catch (ParseException e) {
	dlg.setTitle(longDateFormat.format(c.getTime()));	// New -- update dialog title
	dlg.updateDate(year, month, day);
}
...

The date picker dialog now displays the title with the correct date as shown below:

Please follow and like us:

2 Replies to “Android Date Picker Dialog Weirdness”

  1. I'm a biginner of developing Android App.
    The above article is just fit to what I need.
    May I get the full source of the example you made.
    Thanks a lot and have a nice day!!

Leave a Reply

Your email address will not be published. Required fields are marked *