How to limit on DatePicker to cannot choose date in future. I have DatePicker in activity which is used for registration. What to do so user cannot pick date of birthday if user has less that 18 years ( for example cannot pick date after today_date - 18 years )?
You didn’t mention which API level. On Honeycomb and later, DatePicker has setMin/MaxDate methods you can use to restrict the permissible range.
I hope you got the answer till now. Following might be useful to others as well: If you want to disable user to pick a date 18 years after today, here is what you may do.
Get time from 1-JAN-1970 to 18 years from now (in milli seconds)
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR)+18,cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), 0);
long time = cal.getTimeInMillis();
set it as max allowed date in datepicker
datepicker2.setMaxDate(time);
Hope it helps to anyone else looking for a answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With