prolificinteractive / material-calendarview

A Material design back port of Android's CalendarView
https://prolificinteractive.github.io/material-calendarview/
MIT License
5.91k stars 1.32k forks source link

Multiple dot span to one date #1145

Open mimakopanja opened 10 months ago

mimakopanja commented 10 months ago

I have database with lists of tasks, i need to add multiple dot spans to one date with different color (color depends on the list category - study, work...)

Spans are adding but they are overlapping each other. Can anyone please help?

kaiyrzhanDE commented 8 months ago

Hi. How you solve it?

LeBogoo commented 4 months ago

I've had almost the same problem, so I've written this class for single color dots.

I hope this is helps anyone in the future:

import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.style.LineBackgroundSpan;

public class MultiDotSpan implements LineBackgroundSpan {
    private static final float DEFAULT_RADIUS = 3;
    private static final float DEFAULT_PADDING = 15;

    private final float radius;
    private final int color;
    private final int amount;

    public MultiDotSpan() {
        this.radius = DEFAULT_RADIUS;
        this.color = 0;
        this.amount = 1;
    }

    public MultiDotSpan(float radius) {
        this(radius, 0);
    }

    public MultiDotSpan(float radius, int color) {
        this(radius, color, 1);
    }

    public MultiDotSpan(float radius, int color, int amount) {
        this.radius = radius;
        this.color = color;
        this.amount = amount;
    }

    @Override
    public void drawBackground(Canvas canvas, Paint paint, int left, int right, int top, int baseline, int bottom, CharSequence charSequence, int start, int end, int lineNum) {
        int oldColor = paint.getColor();
        if (color != 0) {
            paint.setColor(color);
        }
        float center = (left + right) / 2f;
        for (int i = 0; i < amount; i++) {
            float x = center + (i - (amount - 1) / 2f) * DEFAULT_PADDING;
            canvas.drawCircle(x, bottom + radius, radius, paint);
        }
        paint.setColor(oldColor);
    }
}