material-calendarview icon indicating copy to clipboard operation
material-calendarview copied to clipboard

How to show span across date range?

Open manideepla opened this issue 5 years ago • 6 comments

Like how Google's Material Date Picker shows?

manideepla avatar Nov 25 '19 12:11 manideepla

Can somebody help with this please?

manideepla avatar Dec 10 '19 10:12 manideepla

In xml use

    <com.prolificinteractive.materialcalendarview.MaterialCalendarView
        android:id="@+id/calendar_view_range"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:mcv_selectionMode="range"
        />

dri94 avatar Dec 26 '19 15:12 dri94

In xml use

    <com.prolificinteractive.materialcalendarview.MaterialCalendarView
        android:id="@+id/calendar_view_range"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:mcv_selectionMode="range"
        />

That just allows me to select a range of dates. What I wanted is something like this: calendar

manideepla avatar Dec 28 '19 16:12 manideepla

If you just want basic highlighting (without the handles and rounded corners), it is easy.

  1. Create a drawable for background

drawable/bg_selected_date:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="@color/primaryLightColor"/>
</shape>
  1. Create a state list for background.

drawable/sl_bg_selected_date:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_selected_date" android:state_checked="true" />
    <item android:drawable="@android:color/transparent" />
</selector>
  1. Create event decorator for selected dates. The code below will apply provided decorator to all selected dates.
    class SelectionDecorator(private val selectionDrawable: Drawable): DayViewDecorator {
        override fun shouldDecorate(day: CalendarDay?) = true
    
        override fun decorate(view: DayViewFacade?) {
            view?.setSelectionDrawable(selectionDrawable)
        }
    }
  1. Add Event decorator to calendar view
calendar.addDecorator(SelectionDecorator(
    context.getDrawable(R.drawable.sl_bg_selected_date) 
        ?: throw Exception("No selection drawable")
))

Now selected range appears as below: Screenshot_20200515-095948

adityabhaskar avatar May 15 '20 09:05 adityabhaskar