I' ve got two datepickers in my activity:
DatePicker DatePickerDateStart = (DatePicker) findViewById(R.id.datestart);
DatePicker DatePickerDateEnd = (DatePicker) findViewById(R.id.dateend);
I use spinner mode (datePickerMode) for both and the calendar (calendarViewShown) is hidden.
The 1st datepicker holds the "From" date and the 2nd the "To" date. I am trying to make the 2nd datepicker setMinDate()
when the 1st datepicker changes value. So far I have this:
Calendar calendar = Calendar.getInstance();
DatePickerDateStart.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker arg0, int year, int month, int day) {
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
try {
Date mDate = sdf.parse(year + "-" + (month+1) + "-" + day);
long timeInMilliseconds = mDate.getTime();
DatePickerDateEnd.setMinDate(timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
My issue is that setMinDate()
works the 1st time the user selects a date from the "From" datepicker, but it doesn't set a min date after that (i.e. on a second change).
I noticed though, that it will setMinDate()
again only if I change the year from the "From" datepicker. Then it sets a new min date again to the "To" datepicker. So I can get the desired behavior if I change the year firstly and then the rest of the spinners (day,month). But this is not right I guess, because the correct behavior will be to set a min date whenever a change is happening to any of the spinners (month,day,year). However timeInMilliseconds
holds the new date on every change but setMinDate()
does not update the "To" datepicker.
Any help on that? I hope I've made myself clear