androidandroid-calendar

How to set different colors for multiply selected dates?


I use com.prolificinteractive.materialcalendarview.MaterialCalendarView in my app. Is it possible to set different colors for multiply selected dates? Or I need use custom calendarView? thanks.


Solution

  • You should be able to set different decorators for each day of the week. Each decorator will have a different selector associated to it.

    For example:

    mcv.addDecorators(
                new MondayDecorator(this),
                new TuesdayDecorator(),
                ...
        );
    
    public class MondayDecorator implements DayViewDecorator {
    
        private final Calendar calendar = Calendar.getInstance();
        private final Drawable drawable;
    
        public MySelectorDecorator(Activity context) {
            drawable = context.getResources().getDrawable(R.drawable.monday_selector);
        }
    
        @Override
        public boolean shouldDecorate(CalendarDay day) {
            day.copyTo(calendar);
            int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
            return weekDay == Calendar.MONDAY;
        }
    
        @Override
        public void decorate(DayViewFacade view) {
            view.setSelectionDrawable(drawable);
        }
    }
    

    and finally your monday_selector:

        <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"
        android:exitFadeDuration="@android:integer/config_shortAnimTime">
    
        <item android:state_checked="true"
            android:drawable="@drawable/ic_monday_selector" />
    
        <item android:state_pressed="true"
            android:drawable="@drawable/ic_monday_selector" />
    
        <item android:drawable="@android:color/transparent" />
    
    </selector>
    

    I haven't tried it but this should work.