Android DatePickerDialog: Set min and max date for selection -
i know there quite lot of question none of solutions working me, question. want restrict user select date before today, not able so.
public class datepickerdialogfragment extends dialogfragment { private ondatesetlistener listener; public void setlistener(ondatesetlistener listener) { this.listener = listener; } @nonnull @override public dialog oncreatedialog(bundle savedinstancestate) { calendar calendar = calendar.getinstance(); int year = calendar.get(calendar.year); int month = calendar.get(calendar.month); int day = calendar.get(calendar.day_of_month); datepickerdialog dialog = new datepickerdialog(getcontext(), listener, year, month, day); dialog.getdatepicker().setmindate(calendar.gettimeinmillis()); return dialog; } }
i showing as:
datepickerdialogfragment fragment = new datepickerdialogfragment(); fragment.setlistener(datesetlistener); fragment.show(getsupportfragmentmanager(), "choose booking date");
i want user should not able select date before today. can see called setmindate()
method today's time no effect. dialog shows dates before today grayed selectable.
i tried sub-class datepickerdialog
, override ondatechanged
suggested in stackoverflow answers without success.
try method
@nonnull @override public dialog oncreatedialog(bundle savedinstancestate) { calendar calendar = calendar.getinstance(); int year = calendar.get(calendar.year); int month = calendar.get(calendar.month); int day = calendar.get(calendar.day_of_month); datepickerdialog dialog = new datepickerdialog(getcontext(), listener, year, month, day); field mdatepickerfield; try { mdatepickerfield = dialog.getclass().getdeclaredfield("mdatepicker"); mdatepickerfield.setaccessible(true); } catch (exception e) { e.printstacktrace(); } dialog.getdatepicker().setmindate(system.currenttimemillis() - 1000); return dialog; }
instead of your
@nonnull @override public dialog oncreatedialog(bundle savedinstancestate) { calendar calendar = calendar.getinstance(); int year = calendar.get(calendar.year); int month = calendar.get(calendar.month); int day = calendar.get(calendar.day_of_month); datepickerdialog dialog = new datepickerdialog(getcontext(), listener, year, month, day); dialog.getdatepicker().setmindate(calendar.gettimeinmillis()); return dialog; }
edit1:
i have faced issue user can select not-selectable dates in android l 5.0.2
. there bug reported here. solved in android l 5.1.0
.
for temporary solution of issue can compare selected date current system date , put condition based on that. used workaround
edit2:
add ondatesent()
method in datepickerdialogfragment
, check if it's earlier date set in setmindate()
. if so, show datepickerdialog
again.
final long today = system.currenttimemillis() - 1000; @override public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { calendar calendar = calendar.getinstance(); calendar.set(year, monthofyear, dayofmonth); //if user tries select date in past (or today) if (calendar.gettimeinmillis() < today) { //make them try again datepickerdialogfragment fragment = new datepickerdialogfragment(); fragment.setlistener(datesetlistener); fragment.show(getsupportfragmentmanager(), "choose booking date"); toast.maketext(this, "invalid date, please try again", toast.length_long).show(); } else { //success } }
Comments
Post a Comment