DatePreference, an Android library

Use this library if you want a DatePreference in your Android app.

This is available as source code, a JAR, and a dex file.

Screenshot showing the date preference for selecting the date of birth

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!

About these ads

13 Comments

  1. Chris Meiklejohn
    Posted September 28, 2010 at 12:19 pm | Permalink

    Very nice!

  2. Rodrigo Ziviani
    Posted October 24, 2010 at 6:14 pm | Permalink

    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?

  3. Posted October 24, 2010 at 8:41 pm | Permalink

    Rodrigo, great pont. The fact that the tests haven’t caught this means this code has to go. Thanks!

  4. JimS
    Posted November 3, 2010 at 11:54 pm | Permalink

    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

    • JimS
      Posted November 3, 2010 at 11:59 pm | Permalink

      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″

    • Posted November 4, 2010 at 12:07 am | Permalink

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

      • JimS
        Posted November 4, 2010 at 11:51 am | Permalink

        Thanks Mike, that will get me started.

  5. Rootko
    Posted November 30, 2010 at 10:15 am | Permalink

    Awesome, thanks

  6. Joe
    Posted January 4, 2011 at 1:36 am | Permalink

    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!

  7. Thomas Leslie
    Posted March 16, 2011 at 2:42 am | Permalink

    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.

  8. Thomas Leslie
    Posted March 16, 2011 at 3:10 am | Permalink

    Sorry, please ignore my previous comment :S :)

  9. JL
    Posted August 6, 2012 at 9:34 pm | Permalink

    Thanks a lot! This is great

  10. Timothy
    Posted August 25, 2012 at 11:49 am | Permalink

    Thank you so much! It has helped me alot.


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: