How do I change the background color of calendar dates in MaterialCalendarView

家住魔仙堡 提交于 2019-12-05 19:09:02
David Rawson

The first step is to create a DayViewDecorator that will take as parameters a Date and a color:

public class EventDecorator implements DayViewDecorator {

    private final Drawable drawable;
    private final CalendarDay day;
    private final int color;

    public EventDecorator(MaterialCalendarView view, Date date, int color) {
        this.day = CalendarDay.from(date);
        this.color = color;
        this.drawable = createTintedDrawable(view.getContext(), color);
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        if (this.day.equals(day)) {
            return true;
        }
        return false;
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.setSelectionDrawable(drawable);
    }

    private static Drawable createTintedDrawable(Context context, int color) {
        return applyTint(createBaseDrawable(context), color);
    }

    private static Drawable applyTint(Drawable drawable, int color) {
        Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(wrappedDrawable, color);
        return wrappedDrawable;
    }

    private static Drawable createBaseDrawable(Context context) {
        return ContextCompat.getDrawable(context, R.drawable.day);
    }
}

(N.B. I used the code in this answer to apply the tint. Also, since you didn't specify, I have assumed that the drawable is some kind of image that needs tinting in this way.)

The next step is to create an Event class for storing the events you parse from the API call:

public class Event {

    private Date date;
    private int color;

    public Event(Date date, int color) {
        this.date = date;
        this.color = color;
    }

    public Date getDate() {
        return date;
    }

    public int getColor() {
        return color;
    }
}

Now we need to add logic to your onResponse() method to parse the JSON and add decorators for each event. It is hard to know what exactly to write because you haven't given a sample of the JSON. Your previous questions show that you already know how to parse a Date so I think this will be enough. Since you haven't specified, I'll leave it out for now. Also, I'm just appending to your code - I won't try and refactor it much.

@Override
public void onResponse(String response) {
    JSONObject object = null;
    try {
        object = new JSONObject(response);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONArray jsonarray = null;
    try {
        jsonarray = object.getJSONArray("Table");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    SimpleDateFormatter formatter = new SimpleDateFormatter(); //TODO: update this line with the correct formatter
    List<Event> events = new ArrayList<>();
    for (int i = 0; i < jsonarray.length(); i++) {
        try {
            JSONObject obj = jsonarray.getJSONObject(i);
            String str2 = obj.optString("eventdate");
            String str1 = obj.optString("eventcolor"); 
            Date date = formatter.parse(str2);
            int color = Integer.parseInt(str1); //TODO: update this line with the correct code to parse your color
            Event event = new Event(date, color);
            events.add(event);
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    for (Event event : events) {
        EventDecorator eventDecorator = new EventDecorator(calendarView, event.getDate(), event.getColor());
        calendarView.addDecorator(eventDecorator);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!