Android datepicker min max date before api level 11
android, android-datepicker
Solution
You can set range with init datePicker method. Example with min value :
// Calendar
this.calendar = new GregorianCalendar();
this.datePicker = (DatePicker) findViewById(R.id.xxxxx);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// (picker is a DatePicker)
this.datePicker.setMinDate(this.calendar.getTimeInMillis());
} else {
final int minYear = this.calendar.get(Calendar.YEAR);
final int minMonth = this.calendar.get(Calendar.MONTH);
final int minDay = this.calendar.get(Calendar.DAY_OF_MONTH);
this.datePicker.init(minYear, minMonth, minDay,
new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int month, int day) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, month, day);
if (calendar.after(newDate)) {
view.init(minYear, minMonth, minDay, this);
}
}
});
Log.w(TAG, "API Level < 11 so not restricting date range...");
}
Problem
I am trying to set the min and max date of the date picker in Android to before API level 11. I used the following code: ``` mDatePickerField = startDatePickerDialog.getClass().getDeclaredField("mDatePicker"); mDatePickerField.setAccessible(true); DatePicker startDatePickerInstance =(DatePicker)mDatePickerField.get(startDatePickerDialog); startDatePickerInstance.init(mYearMin, mMonthMin, mDayMin, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) { Date maxDate = new Date(mYearMax, mMonthMax, mDayMax, 0, 0, 0); Date selectedDate = new Date(i, i1, i2, 0, 0, 0); if (selectedDate.after(maxDate)) { datePicker.updateDate(mYearMax, mMonthMax, mDayMax); } } } ``` However, the `updateDate` method fires `onDateChanged` again and the date picker is not updated. Can anyone help to solve the problem?