Reputation: 229
How to restrict date picker from accepting current and future dates in android i am using google api...any idea..?
Upvotes: 0
Views: 3815
Reputation: 736
Since API level 11 there is a method for that:
DatePicker.setMaxDate(long maxDate)
If it has to work in previous versions, use this method:
public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
You could pass your own OnDateChangedListener which "resets" invalid dates to the newest valid one:
DatePicker picker = ...
int year = ...
int monthOfYear = ...
int dayOfMonth = ...
picker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// check if current date is OK
boolean dateOk = ...
if (!dateOk) {
// correct the date, but be sure the corrected date is OK
// => otherwise you might get endless recursion
year = ...
monthOfYear = ...
dayOfMonth = ...
// update the date widget with the corrected date values
view.updateDate(year, monthOfYear, dayOfMonth);
}
}
});
Upvotes: 3