Use this library if you want a DatePreference in your Android app.
This is available as source code, a JAR, and a dex file.

Use it just like any other preference in your PreferenceScreen XML:
<org.bostonandroid.datepreference.DatePreference
android:key="dob" android:title="@string/dob"
android:defaultValue="1991.01.01" />
There are also convenience methods for getting the selected value, setting the value, and so on. Full API documentation is available.
DatePreference was written during Boston Android hackfests by Mike Burns with help from other Boston Android developers. Feedback welcome!
13 Comments
Very nice!
Hi Mike,
Thanks for the code, it was really useful as model for me to create other customized preferences. I just have one question
On DatePreference.java, method onRestoreInstanceState, lines 137 to 140:
137 if (state == null || !state.getClass().equals(SavedState.class)) {
138 super.onRestoreInstanceState(state);
139 setTheDate(((SavedState) state).dateValue);
140 } else {
———–
On this part you’re checking if state is null or if it’s from the SavedState class, after that you call super on line 138 and finally setTheDate on 139
It seems to me that line 139 will always fail: if state is null, it will fail with NPE. If state is not of the type SavedState class, this line will also fail with a ClassCastException. Am I missing something here?
Rodrigo, great pont. The fact that the tests haven’t caught this means this code has to go. Thanks!
Hi Mike,
Awesome code, thanks!
Although I have a question… With your date preference, the currently selected date always shows up where the “android:summmary” shows up. I like that, and would like that for the EditText preference as well. How can that be done by default?
For example, with this preferences.xml:
It shows in the preference screen as:
Event Name Title
Event Name Summary
———————-
Event Date Title
December 25, 2011
———————-
Event Time
Event Time Summary
———————-
and I would like the “Event Name Summary” to show the value of eventDate1 instead.
Thanks,
Jim
Sorry, my xml code disappeared in the post above. so here it is with stuff removed so it doesn’t disappear…
EditTextPreference
android:key=”eventName1″
android:title=”@string/eventName1Title”
android:summary=”@string/eventName1Summary”
DatePreference
android:key=”eventDate1″
android:title=”@string/eventDate1Title”
android:summary=”@string/eventDate1Summary”
android:defaultValue=”2011.12.25″
TimePickerPreference
android:key=”eventTime1″
android:title=”@string/eventTime1Title”
android:summary=”@string/eventTime1Summary”
android:defaultValue=”00:00:00″
Hi Jim,
I have nothing for you to copy and paste, but the basic idea is to use the DatePreference’s .summaryFormatter() and the EditText’s .setSummary().
Look at DatePreference.persistDate() for an example.
editText.setSummary(summaryFormatter().format(datePreference.getDate().getTime()))
Thanks Mike, that will get me started.
Awesome, thanks
Great post, but how do you fix the issue @ line 139 reported earlier in the comments:
137 if (state == null || !state.getClass().equals(SavedState.class)) {
138 super.onRestoreInstanceState(state);
139 setTheDate(((SavedState) state).dateValue);
140 } else {
Thanks!
Hi!
I think you can improve the code with the following snippet
@Override
protected void onDialogClosed(boolean shouldSave) {
super.onDialogClosed(shouldSave);
if (shouldSave && this.changedValueCanBeNull != null) {
callChangeListener(this.changedValueCanBeNull);
setTheDate(this.changedValueCanBeNull);
this.changedValueCanBeNull = null;
}
}
I needed to call the callChangeListener, otherwise i wasn’t able to use the onPreferenceChange(Preference arg0, Object arg1) method.
Sorry, please ignore my previous comment :S :)
Thanks a lot! This is great
Thank you so much! It has helped me alot.