Using date range with the DatePickerDialog in Android API below Level 11
- juillet
- 06
If you plan to use a date picker into your application without re-coding the whole thing, Android API does the job for you with a useful already made dialog: DatePickerDialog.
Here is a tutorial to use it: http://developer.android.com/resources/tutorials/views/hello-datepicker.html
Nevertheless, limiting the choice of dates (for example not before or not after today) is not direct by using a API bellow level 11 (Android 3.0). And once a solution has been chosen, it also has to work for the newest APIs.
After API Level 11, it’s easy after the creation of the dialog to get the date picker, and then set a minimum or maximum date, it would look like this:
mMonth, mDay);
DatePicker dp = d.getDatePicker();
dp.setMaxDate(maxDate);
return d;
Unfortunately, for now, deployed versions of Android in devices are still mostly around API Level 7 (Android 2.1) and perhaps Android 2.3.3 (Level 10).
It means that it’s not possible to access to the getDatePicker() and setMaxDate() methods.
One solution would be to have some special code for Api levels before 11, and use these methods for the next ones, but it can be more complex to manage different levels of compilation if the need is only for a date picker.
The solution below makes things work with all API levels (tested at least starting from level 7).
The principle is to override the DatePickerDialog object in order to catch the OnDateChanged event and maintain the date picker element values between the desired ranges.
private Calendar minDate;
private Calendar maxDate;
private java.text.DateFormat mTitleDateFormat;
public LimitedRangeDatePickerDialog (Context context, DatePickerDialog.OnDateSetListener callBack,
super(context,callBack,year,monthOfYear,dayOfMonth);
this.minDate=minDate;
this.maxDate=maxDate;
mTitleDateFormat = java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL);
}
Calendar newDate=Calendar.getInstance();
newDate.set(year, month,day);
if(minDate!=null && minDate.after(newDate)) {
view.init(minDate.get(Calendar.YEAR),minDate.get(Calendar.MONTH),
setTitle(mTitleDateFormat.format(minDate.getTime()));
} else if(maxDate!=null && maxDate.before(newDate)) {
view.init(maxDate.get(Calendar.YEAR),maxDate.get(Calendar.MONTH),
setTitle(mTitleDateFormat.format(maxDate.getTime()));
} else {
view.init(year, month, day,this);
setTitle(mTitleDateFormat.format(newDate.getTime()));
}
}
}
The setTitle isn’t mandatory but the behavior existed in the Api level 7 and disappeared after, it might be useful on a smartphone if the user needs to know the day of week of the selected day. On tablets, a calendar is shown, so it’s easier to have the information of the selected day of week but it doesn’t disturb the user experience and still enriching it to have the full date displayed as on smartphones in the title.

Can you please email me the code for this script or please inform me in detail regarding this script?
I noticed the new veisorn of the datepicker. It looks very good. Right now, I’m sticking to the tested production quality verison, but we’ll probably switch later. ThemeRoller! That’s a great advancement
Thanks!
Interesting…
Hello,first of all: thanks a lot for this gem of code ! =)Utilising DateSlider I’m nnticiog the existence of a problem (probably caused by my stupidity): when the widget appears, even if I set the min and max interval (see the code below), it always shows me the current date. My idea is instead that the widget should show the lower limit of the range, not the current date. Where I’m wrong ?final Calendar startDate = Calendar.getInstance();startDate.set(Calendar.DAY_OF_MONTH, 15);startDate.set(Calendar.MONTH, 2);startDate.set(Calendar.YEAR, 2009);final Calendar maxDate = Calendar.getInstance();maxDate.set(2009, 4, 30);return new MonthYearDateSlider( … );Thanks in advance!
Nice article. Thanks for sharing this exciting post with us. I enjoy at the time of reading this post. You have done a great job by sharing this post with us. Keep it up.