17

I'm targeting for and building with API level 21, using AppCompat v21.

It gives me a nicely looking new date picker, which has the unexpected property of allowing me to choose a future date when max date has been set using

datePicker.setMaxDate(Calendar.getInstance().getTimeInMillis())

The future dates are greyed out, but I can still choose any one of them. Is that a bug? Am I doing it wrong? Is there a way to prevent the user from being able to pick a future date in the date picker?

The old Holo date picker did not allow picking a future date when setting a maximum date.

UPDATE:

While it is not working properly on my Nexus 4 running stock 5.0, it is working properly on my Nexus 6 running stock Android 5.1.1. Perhaps it was a bug in Android 5.0 and it was fixed in 5.1? Can anyone confirm?

Divisible by Zero
  • 2,616
  • 28
  • 31

4 Answers4

22

So to answer my own question:

I've looked at the Android source of DatePickerCalendarDelegate.java at grepcode, specifically at public void setMaxDate(long maxDate) for both Android versions 5.0 and 5.1.

What's new in 5.1 in setMaxDate() is that

mDayPickerView.goTo(getSelectedDay(), false, true, true);

has been changed to:

mDayPickerView.setMaxDate(maxDate);

It seems they fixed it there, which corresponds with my observation that it works as expected in 5.1 but not in 5.0.

And so it seems we're stuck with dealing with the fact that it doesn't work properly in Android 5.0 (days after max date are greyed out but can still be chosen).

Divisible by Zero
  • 2,616
  • 28
  • 31
  • so what do we do ? – Sagar Nayak Mar 25 '16 at 13:43
  • What I did was simply ignore the selected date if the user chose a date in the future and show an error message (snackbar or toast). Another solution could also be to use another datepicker, perhaps https://github.com/wdullaer/MaterialDateTimePicker – Divisible by Zero Mar 25 '16 at 14:50
  • In onDateSet() we can put a check like this Calendar calendar = Calendar.getInstance(); calendar.set(year, monthOfYear, dayOfMonth); Calendar calendar1 = Calendar.getInstance(); if (calendar.getTime().after(calendar1.getTime())) { Toast.makeToast(getApplicationContext,"Please select a valid date,Toast.LENGTH_LONG).show(); return; } – MrDumb Jul 14 '16 at 06:52
2

I am using this and its working correctly

Call this function to open date picker

public void openDatePicker() {
        DatePickerDialog dpd = new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
        dpd.getDatePicker().setMaxDate(System.currentTimeMillis());
        dpd.show();
    }

Here is the mDateSetListener

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            date.setText(dayOfMonth + " / " + (monthOfYear + 1) + " / " + year); // Here date is a TextView to display date
            date.setVisibility(View.VISIBLE);
        }
    };
Azim Ansari
  • 1,378
  • 11
  • 20
-1

You can use a method in which u can pass the value of the date entered by the user and compare it for example to the current date , if it's a future date u can reset the value of date used to a specific one (for example the current date) and show a toast to inform him.

  • 1
    Thanks, I realize I can do that, but I didn't have to do so before (with the old date picker), and I find it odd that I'd have to that now. I'd like to know if i can get the date picker to not allow picking future dates (and if not, I'd like to know if the Android team has done this on purpose, or if it's a bug) – Divisible by Zero Feb 11 '15 at 16:38
-2

you can work around it on your CSS by finding the right element (with a "disabled" class) and setting a pointer events to none.`

.bs-datepicker-body table td.disabled {
      pointer-events: none;
}
Victor
  • 1