Davide Davide -4 years ago 117
Android Question

setSelecteableDays Material DateTime Picker android

I'm trying to use the Material

DateTimePicker
library.

The problem is that when I set a
minDate
and
maxDate
in the
datePicker
and an array of selectable dates the
dataPicker
does not show what I'm aspected

Calendar minDate = Calendar.getInstance();
minDate.set(Calendar.YEAR, 2016);
minDate.set(Calendar.MONTH, 5);
minDate.set(Calendar.DAY_OF_MONTH, 1);

Calendar maxDate = Calendar.getInstance();
maxDate.set(Calendar.YEAR, 2016);
maxDate.set(Calendar.MONTH, 10);
maxDate.set(Calendar.DAY_OF_MONTH, 30);

Calendar aDate = Calendar.getInstance();
maxDate.set(Calendar.YEAR, 2016);
maxDate.set(Calendar.MONTH, 6);
maxDate.set(Calendar.DAY_OF_MONTH, 26);

Calendar aDate1 = Calendar.getInstance();
maxDate.set(Calendar.YEAR, 2016);
maxDate.set(Calendar.MONTH, 5);
maxDate.set(Calendar.DAY_OF_MONTH, 22);

Calendar[] availableDate = new Calendar[2];
availableDate[0] = aDate;
availableDate[1] = aDate1;

DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(ProvaDatePicker.this, availableDate[0].get(Calendar.YEAR), availableDate[0].get(Calendar.MONTH), availableDate[0].get(Calendar.DAY_OF_MONTH));

datePickerDialog.setMinDate(minDate);
datePickerDialog.setMaxDate(maxDate);
datePickerDialog.setSelectableDays(availableDate);
datePickerDialog.setTitle("Available date");
datePickerDialog.show(getFragmentManager(), "title");


This is the resultenter image description here

Answer Source

You are setting the dates in your code with YEAR to 2016, then in your screenshot you show that the YEAR is 2017.

To see the two enabled days you need to go to May 2016 or change the YEAR of your Calendar instances

You have the wrong assignement:

 Calendar aDate = Calendar.getInstance();
 maxDate.set(Calendar.YEAR, 2016);
 maxDate.set(Calendar.MONTH, 6);
 maxDate.set(Calendar.DAY_OF_MONTH, 26);

 Calendar aDate1 = Calendar.getInstance();
 maxDate.set(Calendar.YEAR, 2016);
 maxDate.set(Calendar.MONTH, 5);
 maxDate.set(Calendar.DAY_OF_MONTH, 22);

Here you should use aDate and aDate1 assignment, while you are assigning them to maxDate again and again

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download