Anil Shrestha Anil Shrestha - 28 days ago 4
Android Question

How to set color in selected date of Material Calendar View

I have Material Calendar View Like this

private RecyclerView recycler_month_event_lists;
private FirebaseRecyclerAdapter adapter;


and i have assigned it like this

recycler_month_event_lists = (RecyclerView) view.findViewById(R.id.event_recycler_view);
calendar_view = (MaterialCalendarView) view.findViewById(R.id.calendar_view);


and i have listed my events of respective month through FirebaseRecyclerAdapater like this

recycler_month_event_lists.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new FirebaseRecyclerAdapter<Event, EventViewHolder>(Event.class, R.layout.row_event_list,
EventViewHolder.class, mRef.child("events").child("1").child(month)) {

@Override
protected void populateViewHolder(EventViewHolder viewHolder, Event model, int position) {
List<CalendarDay> list = new ArrayList<CalendarDay>();
ArrayList<Integer> day = model.getDate();
ArrayList<Date> markedDates = new ArrayList<>();
Calendar calendar = Calendar.getInstance();

//generating ArrayList<Date>
for (int i = 0; i < day.size(); ++i) {
Calendar cal = Calendar.getInstance();
Integer year = cal.get(Calendar.YEAR);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, day.get(i));
cal.set(Calendar.MONTH, Integer.valueOf(month));
Date newdate = cal.getTime();
markedDates.add(newdate);
}
//adding those generated ArrayList<Dates> in List<CalendarDays>
for (Date date : markedDates) {
// might be a more elegant way to do this part, but this is very explicit
int year = date.getYear();
int month = date.getMonth() -1; // months are 0-based in Calendar
int newday = date.getDay();

calendar.set(year, month, newday);
CalendarDay calendarDay = CalendarDay.from(calendar);
list.add(calendarDay);
}

//adding list of CalendarDays
calendarDays = list;

//code to decorate selected dates
calendar_view.addDecorators(new EventDecorator(Color.parseColor("#00ff00"), calendarDays));



if (!day.isEmpty()) {

for(int i = 0; i < day.size();i++){
int z = i+1;
if(i == 0){
from = "From"+ " " +day.get(i);
}

if(z == day.size()){
to = "to"+ " " +day.get(i);
}
}


}

viewHolder.ev_title.setText(model.getTitle());
viewHolder.ev_description.setText(model.getDescription());
viewHolder.ev_date.setText(from + " " + to);
Glide.with(getActivity()).load(model.getIcon()).into(viewHolder.ev_image);


}
};
recycler_month_event_lists.setAdapter(adapter);

}


here ArrayList day = model.getDate(); returns the day that needs to be colored. the log of day is like this

D/arra: [1, 2, 3]


I followed one of the stack answer but i can get my dates colored i dont know what is wrong with my code.
I used

calendar_view.addDecorators(new EventDecorator(Color.parseColor("#00ff00"), calendarDays));


to decorate selected days
My EventDecorator is like this

public class EventDecorator implements DayViewDecorator {

private final int color;
private final HashSet<CalendarDay> dates;

public EventDecorator(int color, Collection<CalendarDay> dates) {
this.color = color;
this.dates = new HashSet<>(dates);
}

@Override
public boolean shouldDecorate(CalendarDay day) {
return dates.contains(day);
}

@Override
public void decorate(DayViewFacade view) {
view.addSpan(new DotSpan(5, color));
}
}


And my CalendarDays is like this

private Collection<CalendarDay> calendarDays = new Collection<CalendarDay>() {
@Override
public boolean add(CalendarDay object) {
return false;
}

@Override
public boolean addAll(Collection<? extends CalendarDay> collection) {
return false;
}

@Override
public void clear() {

}

@Override
public boolean contains(Object object) {
return false;
}

@Override
public boolean containsAll(Collection<?> collection) {
return false;
}

@Override
public boolean isEmpty() {
return false;
}

@NonNull
@Override
public Iterator<CalendarDay> iterator() {
return null;
}

@Override
public boolean remove(Object object) {
return false;
}

@Override
public boolean removeAll(Collection<?> collection) {
return false;
}

@Override
public boolean retainAll(Collection<?> collection) {
return false;
}

@Override
public int size() {
return 0;
}

@NonNull
@Override
public Object[] toArray() {
return new Object[0];
}

@NonNull
@Override
public <T> T[] toArray(T[] array) {
return null;
}
};


any help would be appreciated. Thanks in advanced.

Answer Source

I have looked at your code an I am pretty confident that the problem lies in the decorate method inside the EventDecorator.

view.addSpan(new DotSpan(5, color));

doesn't work on the Material Dates (or at least it doesn't change the color the way you want it to).

You could use

view.addSpan(new ForegroundColorSpan(color));

or

view.addSpan(new BackgroundColorSpan(color));

instead.

Please make sure that public void decorate(DayViewFacade view) is called for the correct dates. If you can assure that then setting the color span should work. Hope this helps :)