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?
Found a cool way to set the
min
andmax
date forDatePickerDialog
. Works for All Android versions. Simply you need to return theDatePickerDialog
based on Android version