I'm currently using MaterialDateTimePicker library for date picking, and I'm trying to listen when the user select day from the calendar to open another list depends on the day the user selected.
I have used dpd.setOnDateSetListener
and public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth)
. However, both of them are getting triggered only when the user clicks on the OK button and it doesn't trigger right away when the user selects the date.
Is there a way to be able to know when the user selects date?
In the screenshot below, you can see all days in this month are disabled even though the disabled day is selected and if the user clicks OK, it will be enabled. So what I'm trying to do is one of those 3 things:
My problem right now that if the user hit ok even the auto selected day is a disabled day it will pop up to display the available times in that day for ex-mp Wednesday (from 10 to 15)
My Current code to set enabled days and disabled days :
Calendar day;
List<Calendar> weekends = new ArrayList<>();
int weeks = 5;
for (int i = 0; i < (weeks * 7); i = i + 7) {
for (int d = 0; d < slots.getResponse().getDays().size(); d++) {
day = Calendar.getInstance();
switch (slots.getResponse().getDays().get(d).getDay()) {
case "Sunday":
day.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - day.get(Calendar.DAY_OF_WEEK) + i));
weekends.add(day);
break;
case "Monday":
day.add(Calendar.DAY_OF_YEAR, (Calendar.MONDAY - day.get(Calendar.DAY_OF_WEEK) + i));
weekends.add(day);
break;
case "Tuesday":
day.add(Calendar.DAY_OF_YEAR, (Calendar.TUESDAY - day.get(Calendar.DAY_OF_WEEK) + i));
weekends.add(day);
break;
case "Wednesday":
day.add(Calendar.DAY_OF_YEAR, (Calendar.WEDNESDAY - day.get(Calendar.DAY_OF_WEEK) + i));
weekends.add(day);
break;
case "Thursday":
day.add(Calendar.DAY_OF_YEAR, (Calendar.THURSDAY - day.get(Calendar.DAY_OF_WEEK) + i));
weekends.add(day);
break;
case "Friday":
day.add(Calendar.DAY_OF_YEAR, (Calendar.FRIDAY - day.get(Calendar.DAY_OF_WEEK) + i));
weekends.add(day);
break;
case "Saturday":
day.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - day.get(Calendar.DAY_OF_WEEK) + i));
weekends.add(day);
break;
}
}
}
Calendar[] SelectableDays = weekends.toArray(new Calendar[weekends.size()]);
dpd.setSelectableDays(SelectableDays);
Calendar minday = Calendar.getInstance();
minday.add(Calendar.DAY_OF_YEAR, minday.get(Calendar.DAY_OF_MONTH) + 1);
dpd.setMinDate(minday);
/*
Disable Upcoming Daysa
*/
Calendar UpcomingDay;
boolean FirstLoop = true;
List<Calendar> nextDaysList = new ArrayList<>();
for (int i = 0; i < slots.getResponse().getExecTime() - 1; i++) {
UpcomingDay = Calendar.getInstance();
if (FirstLoop) {
UpcomingDay.add(Calendar.DAY_OF_MONTH, i);
i--;
FirstLoop = false;
} else {
UpcomingDay.add(Calendar.DAY_OF_MONTH, i + 1);
}
nextDaysList.add(UpcomingDay);
}
Calendar[] DisabledDays = nextDaysList.toArray(new Calendar[nextDaysList.size()]);
dpd.setDisabledDays(DisabledDays);
There is a workaround that has a direct API provided by the library which is setSelectableDays(Calendar[] days)
. This takes an array of days as the parameter and enables you to enable the days only which you want to have enabled.
Hence, instead of disabling the dates, you may consider enabling it.
While setting up your DatePickerDialog
, you may consider having the first enabled date as default as well, which might solve your problem as well. Initialize with the value that is your first enabled day.
DatePickerDialog dpd = DatePickerDialog.newInstance(
MainActivity.this,
enabledYear, // Initial enabled year selection
enabledMonth, // Initial enabled month selection
enabledDayOfMonth // Inital enabled day selection
);
Hope that solves your problem.