Android: date picker. The current date and future dates should not be accepted.

How to limit date selection from accepting current and future dates in android, I use google api ... any idea ..?

0
source share
2 answers
  • Starting with API level 11, there is a way to do this:

    DatePicker.setMaxDate(long maxDate) 
  • If it should work in previous versions, use this method:

     public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener) 

You can pass your own OnDateChangedListener, which "resets" invalid dates to the last valid:

 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); } } }); 
+3
source

how to not allow the user to select a date in datepicker? This link will only help you accept past dates. ITS WORKING FOR ME !!!!

0
source

All Articles