Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight the current day on "android material-calendar"

I am using material-calendar for android https://github.com/prolificinteractive/material-calendarview

materialCalender = (MaterialCalendarView)v.findViewById(R.id.calendarView);
    materialCalender.state().edit()
            .setFirstDayOfWeek(Calendar.MONDAY)
            .setMinimumDate(CalendarDay.from(1900, 1, 1))
            .setMaximumDate(CalendarDay.from(2100, 12, 31))
            .setCalendarDisplayMode(CalendarMode.MONTHS)
            .commit();

but the calender doesn't show the current day on the calender view. so, how to show that?

like image 266
Hatim Avatar asked Dec 24 '22 22:12

Hatim


2 Answers

try this

    MaterialCalendarView materialCalView = findViewById(R.id.ID_from_your_xml_code);
    materialCalView.setDateSelected(CalendarDay.today(), true);
like image 68
David_G Avatar answered Feb 24 '23 12:02

David_G


If you want to set a different color for the current date you have to add a Decorator to your MaterialCalendarView:

calendarView.addDecorator(new DayViewDecorator() {
    @Override
    public boolean shouldDecorate(CalendarDay day) {
        Calendar cal1 = day.getCalendar();
        Calendar cal2 = Calendar.getInstance();

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

    @Override
    public void decorate(DayViewFacade view) {
        view.setBackgroundDrawable(ContextCompat.getDrawable(MainActivity.this,R.drawable.selector));
    }
});

Where selector is your custom selector.xml, for example:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"
  android:exitFadeDuration="@android:integer/config_shortAnimTime">

 <item
 android:state_checked="true"
 android:drawable="@color/yourColor"
 />

<item
android:state_pressed="true"
android:drawable="@color/yourColor"
/>

<item android:drawable="@android:color/transparent" />
</selector>

Selector Reference Link: https://github.com/prolificinteractive/material-calendarview/blob/master/docs/CUSTOM_SELECTORS.md

Decorator Reference Link: https://github.com/prolificinteractive/material-calendarview/blob/master/docs/DECORATORS.md

like image 26
fgueli Avatar answered Feb 24 '23 11:02

fgueli