Material-calendarview: How to change the text color of other months' dates?

Created on 19 Feb 2019  路  4Comments  路  Source: prolificinteractive/material-calendarview

I have set a different color for date text using

  <com.prolificinteractive.materialcalendarview.MaterialCalendarView
       ...
        app:mcv_dateTextAppearance="@style/CalendarDateTextAppearance"
        app:mcv_showOtherDates="other_months"/>

and the related style

    <style name="CalendarDateTextAppearance" parent="Home">
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">14sp</item>

    </style>

Therefore, all my date texts have the same color. I want my current month date texts to be white and other months' date texts to be grey. How can I achieve this?

All 4 comments

@quentin41500 Really appreciate your input here

@chrismabotuwana Have you found a solution?


Okay, I fixed that.
I created OtherDaysDecorator for other days and DayDecorator for month days:

public class DayDecorator implements DayViewDecorator {
    private Context context;
    private MaterialCalendarView calendarView;

    public DayDecorator(Context context, MaterialCalendarView calendarView) {
        this.context = context;
        this.calendarView = calendarView;
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        Calendar cal1 = day.getCalendar();
        Calendar cal2 = calendarView.getCurrentDate().getCalendar();

        return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new TextAppearanceSpan(context, R.style.Day));
    }
public class OtherDaysDecorator implements DayViewDecorator {
    private Context context;
    private MaterialCalendarView calendarView;

    public OtherDaysDecorator(Context context, MaterialCalendarView calendarView) {
        this.context = context;
        this.calendarView = calendarView;
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        Calendar cal1 = day.getCalendar();
        Calendar cal2 = calendarView.getCurrentDate().getCalendar();

        return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new TextAppearanceSpan(context, R.style.OtherDay));
    }
}



md5-afcd043094845654d01b81ae2870263f



<style name="Day">
        <item name="android:textColor">@color/calendar_day_text_color</item>
</style>



md5-a500580d979d87b38871c233270c2960



<style name="OtherDay">
        <item name="android:textColor">@color/calendar_otherday_text_color</item>
</style>



md5-9db5763e3890bfcc8f6c4f6d9cc3fef6



<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:color="@color/colorBackground"
        android:state_checked="true"/>
    <item
        android:color="@color/colorOnSurface"
        android:state_pressed="true"/>
    <item
        android:color="@color/greyMaterial"
        android:state_enabled="false"/>
    <item android:color="@color/colorOnSurface"/>
</selector>



md5-0443028708a6a3e18d12831337a71f35



<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:color="@color/colorBackground"
        android:state_checked="true"/>
    <item
        android:color="@color/colorOnSurface"
        android:state_pressed="true"/>
    <item
        android:color="@color/greyMaterial"
        android:state_enabled="false"/>
    <item android:color="@android:color/holo_orange_dark"/>
</selector>



md5-1fffde4de14e7095db018ad6b14a5dde



calendarView = view.findViewById(R.id.records_calendar);
DayDecorator dayDecorator = new DayDecorator(getContext(), calendarView);
calendarView.addDecorator(dayDecorator);

calendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
            @Override
            public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
                Log.i(MainScreenActivity.TAG, "onMonthChanged: ");
                calendarView.removeDecorator(todayDecorator);
                calendarView.removeDecorator(daysDecorator);
                calendarView.removeDecorator(otherDaysDecorator);
                calendarView.invalidateDecorators();
                calendarView.addDecorator(daysDecorator);
                calendarView.addDecorator(otherDaysDecorator);
                calendarView.addDecorator(todayDecorator);
            }
        });

        otherDaysDecorator = new OtherDaysDecorator(getContext(), calendarView);
        calendarView.addDecorator(otherDaysDecorator);

        todayDecorator = new TodayDecorator(getContext());
        calendarView.addDecorator(todayDecorator);

Calendar cal1 = day.getCalendar();
Calendar cal2 = calendarView.getCurrentDate().getCalendar();

These 2 lines give an error, unable to find the get calendar

Was this page helpful?
0 / 5 - 0 ratings