Highlight previous selected dates in Material Date Picker Android

243 Views Asked by At

I am trying to highlight the previous selected dates ranges in Material Date Picker Android. I am saving previous months ranges in shared preferences. I want whenever user opens the calendar I can show previous ranges in some way, like highlighted or changed colour. In short, I want to somehow represent the previous selected date ranges. As far I've researched, I cannot select more than one range in material date range picker.

Solution I tried:

I've made previous selected dates inValid due to which they are grayed out as seen in screenshot. I am unable to customise the inValid Dates also.

Screenshot

private void showCalendar() {
    long l1 = prefsManager.getPeriodStartingDayLong();
    long l2 = prefsManager.getPeriodEndingDayLong();
   /* long l1 = 1681412400000L;
    long l2 = 1681498800000L;
    long l3 = 1681585200000L;
    long l4 = 1681671600000L;*/

    MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
    builder.setTitleText("Select Dates");
    builder.setTheme(R.style.MyMaterialCalendarTheme);

    List<Long> highlightedDates = new ArrayList<>();

    highlightedDates.add(l1);
    highlightedDates.add(l2);
  /*  highlightedDates.add(l3);
    highlightedDates.add(l4);*/
    // Add more dates as needed

    CalendarConstraints.DateValidator dateValidator = new CalendarConstraints.DateValidator() {
        @Override
        public boolean isValid(long date) {
            return !highlightedDates.contains(date);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            // Not needed for this example
        }
    };

    CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
    constraintsBuilder.setValidator(dateValidator);

    builder.setCalendarConstraints(constraintsBuilder.build());

    MaterialDatePicker<Pair<Long, Long>> datePicker = builder.build();
    datePicker.show(getSupportFragmentManager(), "DatePickerTag");

}

I was expecting to be able to somehow modify the colours or put a stroke around in-valid dates.

0

There are 0 best solutions below