Kat
Kat

Reputation: 4695

Make date picker inclusive on max date

I'm trying to use an Android date picker to select a date range via two inputs. The start date has the end date as its max date and the end date has the start date as the min date.

I want to allow the date pickers to be set to the same date, to enable selecting a single day as the range (since the range is inclusive).

But it seems that min date is inclusive and max date is always exclusive? Is there any way at all to enable the max date to be inclusive? Right now the date picker styles the end date as if it were selectable, but it's not (which I really hate because that's confusing as hell). It ends up looking like this (to allow Feb 28 as the max selectable date -- note how Mar 1 looks like it's selectable, but it's not):

Appearance

I'm not doing anything special in my code. Just creating a DatePickerDialog and then calling dialog.getDatePicker().setMaxDate(maxDateInMs).

Upvotes: 3

Views: 808

Answers (1)

Linh
Linh

Reputation: 60989

I realize that both minday and maxday are inclusive

final Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
    }
};

DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, date, myCalendar
        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
        myCalendar.get(Calendar.DAY_OF_MONTH));

datePickerDialog.getDatePicker().setMinDate(myCalendar.getTimeInMillis());
datePickerDialog.getDatePicker().setMaxDate(myCalendar.getTimeInMillis()+ DateUtils.DAY_IN_MILLIS);

datePickerDialog.show();

enter image description here

Upvotes: 3

Related Questions