diff --git a/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java b/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java index c364a9f1..c816d7cb 100644 --- a/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java +++ b/library/src/main/java/com/prolificinteractive/materialcalendarview/MaterialCalendarView.java @@ -15,6 +15,7 @@ import android.support.v4.view.ViewPager; import android.support.v7.widget.AppCompatTextView; import android.util.AttributeSet; +import android.util.Log; import android.util.SparseArray; import android.util.TypedValue; import android.view.Gravity; @@ -1475,19 +1476,29 @@ protected void onDateClicked(@NonNull CalendarDay date, boolean nowSelected) { } break; case SELECTION_MODE_RANGE: { - adapter.setDateSelected(date, nowSelected); - if (adapter.getSelectedDates().size() > 2) { - adapter.clearSelections(); - adapter.setDateSelected(date, nowSelected); // re-set because adapter has been cleared + final List currentSelection = adapter.getSelectedDates(); + + if (currentSelection.size() == 0) { + // Selecting the first date of a range + adapter.setDateSelected(date, nowSelected); dispatchOnDateSelected(date, nowSelected); - } else if (adapter.getSelectedDates().size() == 2) { - final List dates = adapter.getSelectedDates(); - if (dates.get(0).isAfter(dates.get(1))) { - dispatchOnRangeSelected(dates.get(1), dates.get(0)); + } else if (currentSelection.size() == 1) { + // Selecting the second date of a range + final CalendarDay firstDaySelected = currentSelection.get(0); + adapter.setDateSelected(date, nowSelected); + if (firstDaySelected.equals(date)) { + // Right now, we are not supporting a range of one day, so we are removing the day instead. + dispatchOnDateSelected(date, nowSelected); + } else if (firstDaySelected.isAfter(date)) { + // Selecting a range, dispatching... + dispatchOnRangeSelected(date, firstDaySelected); } else { - dispatchOnRangeSelected(dates.get(0), dates.get(1)); + // Selecting a range, dispatching in reverse order... + dispatchOnRangeSelected(firstDaySelected, date); } } else { + // Clearing selection and making a selection of the new date. + adapter.clearSelections(); adapter.setDateSelected(date, nowSelected); dispatchOnDateSelected(date, nowSelected); }