7

Hello All: How to disable future dates in DatePickerDialog in Android.

I am using following implementation. http://www.androidpeople.com/android-datepicker-dialog-example

Thanks Ashwani

Ashwani K
  • 7,880
  • 19
  • 63
  • 102
  • I tried to answer this question here: http://stackoverflow.com/questions/7157401/android-date-picker-should-not-accept-current-date-and-future-dates/7159304#7159304 – Markus Wörz Aug 23 '11 at 10:14

1 Answers1

20

You should be able to call getDatePicker().setMaxDate(long) on your DatePickerDialog to set today as your max date. You can update the function with the same name from the snippet you posted.

Note the DatePickerDialog is the object that I referenced in the Android Docs from the link I posted.

@Override
protected Dialog onCreateDialog(int id) {
    Calendar c = Calendar.getInstance();
    int cyear = c.get(Calendar.YEAR);
    int cmonth = c.get(Calendar.MONTH);
    int cday = c.get(Calendar.DAY_OF_MONTH);
    switch (id) {
        case DATE_DIALOG_ID:
        //start changes...
        DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
        dialog.getDatePicker().setMaxDate(new Date().getTime());
        return dialog;
        //end changes...
    }
    return null;
}
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • Thanks Nicholas, please can you tell me where I can put this code to work? I mean in which function I should put this? I am referring to the code which I posted in the question. – Ashwani K May 25 '11 at 06:06
  • 4
    Nicholas, getDatePicker() method is available in API level 11, but my application is targeting API 7+, is there any way to do this in API level 7? – Ashwani K May 26 '11 at 06:46
  • I am not seeing a quick way to do this. Perhaps you will have to create your own CustomDatePicker. – nicholas.hauschild May 26 '11 at 12:45
  • 1
    I added a check in the function to show a toast to the user if he is selecting future dates. :) – Ashwani K May 26 '11 at 15:02
  • In API 21, my date picker does not let me pick a date greater than this month. This month's days after today are grayed out. But I'm still able to pick a grayed out date... – Lou Morda Jan 19 '16 at 22:47