question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to change the text color of other months' dates?

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

2reactions
hifefulcommented, Jul 14, 2020
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));
    }
}

R.style.Day it’s a style with colors for month calendar days.

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

Other days:

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

And my xml files using in these styles:

<?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>
<?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>

In my Fragment I create and add these decorators. Then, on every OnMonthClick I repaint my decorators.

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);
2reactions
chrismabotuwanacommented, Apr 17, 2019

@quentin41500 Really appreciate your input here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changing the color of the text in a cell after a certain date
Select the cell or cells you want to color. Black (Automatic) will be the default font color. On the Home tab of the...
Read more >
Change color when date changes - YouTube
Bored with ZEBRA lines? You can use conditional formatting to create a crazy rule to change color whenever there is new date.
Read more >
change text color on some dates in material calendarview
Add a custom text color on some specific days of months for example change text color of all Sunday to red. make a...
Read more >
Changing the Date Text Color | WordPress.org
I'm trying to change the color of the dates, both the Month and year as well as the day, in the list view....
Read more >
Change the color of text in Numbers on Mac - Apple Support
In the Format sidebar, click the Text tab, then click the Style button near the top of the sidebar. Text sidebar, showing how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found