Yash Sampat
Yash Sampat

Reputation: 30611

Android custom calendar / date picker

This is what the new default DatePickerDialog widget looks like as it appears from Android 5.0 onward:

enter image description here

This is not much liked by my project managers. What they want is the "old style" Holo date picker as it used to be before Marshmallow & Lollipop. Something like this:

enter image description here

I have two options before me:

If I am to create my own widget, then creating the UI is simple enough. My question is, how do I write the business logic for such a calendar, i.e. how do I back it with real data ? How do I account for leap years, and the number of days in different months ? Most importantly, I need the three NumberPickers to be in sync when it comes to the min and max range. It should NOT be possible to scroll beyond the min and max limits. This is the problem I haven't been able to solve.

Here's what I've done so far:

So my question is, is there a pre-existing widget such as this one that I can use ? Alternatively, is there a way to back the NumberPickers with real calendar data, and have them scroll in sync within the min and max date range ? Please guide me. All answers will be appreciated.

EDIT:

While the given answer works, it is worth noting that there is an even better way: wrapping a DatePicker in a DialogFragment.

Upvotes: 2

Views: 5423

Answers (1)

Marten
Marten

Reputation: 3872

You can use the API Date Picker and tell it to use the Holo Design using this constructor: DatePickerDialog (Context, int, DatePickerDialog.OnDateSetListener, int, int, int)

See the comments on this question: DatePicker crash in samsung with android 5.0

You may have to tweak the theme a bit.

Here is how we use this in action: https://github.com/dmfs/opentasks/blob/master/src/org/dmfs/tasks/widget/TimeFieldEditor.java#L437

Upvotes: 1

Related Questions