Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Able to choose future date when setting max date in datepicker

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?

like image 840
Ciske Avatar asked Feb 11 '15 16:02

Ciske


People also ask

How do I restrict date in date picker?

You can restrict the users from selecting a date within the particular range by specifying MinDate and MaxDate properties. The default value of MinDate property is 1/1/1920 and MaxDate property is 12/31/2120 . Dates that appears outside the minimum and maximum date range will be disabled (blackout).

How do you set the minimum and maximum date in input type date?

The min attribute specifies the minimum value (date) for a date field. Tip: Use the min attribute together with the max attribute to create a range of legal values. Tip: To set or return the value of the max attribute, use the max property.

How can I set start and end dates for a Datepicker?

Setting fixed datesvar start_date = new Date ('2013-06-21'); var end_date = new Date('2013-12-24'); Which will set the start date to 21 Juen 2103 and the end date to 24 Dec 2013.


2 Answers

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).

like image 110
Ciske Avatar answered Nov 09 '22 09:11

Ciske


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);
        }
    };
like image 38
Azim Ansari Avatar answered Nov 09 '22 09:11

Azim Ansari