How to prevent Android Material DatePickerDialog from selecting future dates -
i trying create datepickerdialog
not allow user select in future. see many solutions should use setmaxdate(system.currenttimemillis());
not work material dialog thing. prevent user select beyond current month, future dates in month grayed out still selectable. see code below.
datepickerdialog dialog = new datepickerdialog(rootview.getcontext(), new datepickerdialog.ondatesetlistener() { @override public void ondateset(final datepicker datepicker, final int year, final int monthofyear, final int dayofmonth) { datetime = datetime.withyear(year); datetime = datetime.withmonthofyear(monthofyear + 1); datetime = datetime.withdayofmonth(dayofmonth); if (datetime.isafter(datetime.now())) { datetime = new datetime(); snackbar.make(rootview, r.string.future_date_not_allowed, snackbar.length_long).show(); } setdatetime(datetime); } }, datetime.getyear(), datetime.getmonthofyear() - 1, datetime.getdayofmonth()); dialog.getdatepicker().setmaxdate(system.currenttimemillis()); dialog.show();
as might see, using jodatime offload complexity. check once user has selected date, if not in future, though specified using setmaxdate
.
in image below can see today july 23, , i've selected july 29. if press ok, snackbar show. later today grayed, still selectable.... don't me started on timepicker ;)
tl;dr
how can actually prevent user selecting future dates?
you can use prevent future date.
mdatepicker.setmaxdate(system.currenttimemillis());
Comments
Post a Comment