SundeepK / CompactCalendarView

An android library which provides a compact calendar view much like the one used in google calenders.
MIT License
1.52k stars 427 forks source link

Change event text color #328

Open sjla opened 5 years ago

sjla commented 5 years ago

It's really nice library and helpful. but i did not find a solution to change color of that dates who's have events..i tried to achieve this using currentselecteddaytextcolor,but that did not work for me.

Root-vb commented 5 years ago

Hi @sjla try using this app:compactCalendarEventIndicatorStyle="fill_large_indicator" Worked for me.

rlondo commented 5 years ago

With a little modifications to drawEvents, drawMonth functions and Event model this is what I was able to achieve Screenshot_1572260683

`void drawEvents(Canvas canvas, Calendar currentMonthToDrawCalender, int offset) { int currentMonth = currentMonthToDrawCalender.get(Calendar.MONTH); List uniqEvents = eventsContainer.getEventsForMonthAndYear(currentMonth, currentMonthToDrawCalender.get(Calendar.YEAR));

    boolean shouldDrawCurrentDayCircle = currentMonth == todayCalender.get(Calendar.MONTH);
    boolean shouldDrawSelectedDayCircle = currentMonth == currentCalender.get(Calendar.MONTH);

    int todayDayOfMonth = todayCalender.get(Calendar.DAY_OF_MONTH);
    int currentYear = todayCalender.get(Calendar.YEAR);
    int selectedDayOfMonth = currentCalender.get(Calendar.DAY_OF_MONTH);
    float indicatorOffset = bigCircleIndicatorRadius / 2;
    if (uniqEvents != null) {
        for (int i = 0; i < uniqEvents.size(); i++) {
            Events events = uniqEvents.get(i);
            long timeMillis = events.getTimeInMillis();
            eventsCalendar.setTimeInMillis(timeMillis);

            int dayOfWeek = getDayOfWeek(eventsCalendar);
            if (isRtl) {
                dayOfWeek =  6 - dayOfWeek;
            }

            int weekNumberForMonth = eventsCalendar.get(Calendar.WEEK_OF_MONTH);
            float xPosition = widthPerDay * dayOfWeek + paddingWidth + paddingLeft + accumulatedScrollOffset.x + offset - paddingRight;
            float yPosition = weekNumberForMonth * heightPerDay + paddingHeight;

            if (((animationStatus == EXPOSE_CALENDAR_ANIMATION || animationStatus == ANIMATE_INDICATORS) && xPosition >= growFactor ) || yPosition >= growFactor) {
                // only draw small event indicators if enough of the calendar is exposed
                continue;
            } else if (animationStatus == EXPAND_COLLAPSE_CALENDAR && yPosition >= growFactor){
                // expanding animation, just draw event indicators if enough of the calendar is visible
                continue;
            } else if (animationStatus == EXPOSE_CALENDAR_ANIMATION && (eventIndicatorStyle == FILL_LARGE_INDICATOR || eventIndicatorStyle == NO_FILL_LARGE_INDICATOR)) {
                // Don't draw large indicators during expose animation, until animation is done
                continue;
            }

            List<Event> eventsList = events.getEvents();
            int dayOfMonth = eventsCalendar.get(Calendar.DAY_OF_MONTH);
            int eventYear = eventsCalendar.get(Calendar.YEAR);
            boolean isSameDayAsCurrentDay = shouldDrawCurrentDayCircle && (todayDayOfMonth == dayOfMonth) && (eventYear == currentYear);
            boolean isCurrentSelectedDay = shouldDrawSelectedDayCircle && (selectedDayOfMonth == dayOfMonth);

            if (shouldDrawIndicatorsBelowSelectedDays || (!shouldDrawIndicatorsBelowSelectedDays && !isSameDayAsCurrentDay && !isCurrentSelectedDay) || animationStatus == EXPOSE_CALENDAR_ANIMATION) {
                if (eventIndicatorStyle == FILL_LARGE_INDICATOR || eventIndicatorStyle == NO_FILL_LARGE_INDICATOR) {
                    if (!eventsList.isEmpty()) {
                        Event event = eventsList.get(0);
                        drawEventIndicatorCircle(canvas, xPosition, yPosition, event.getColor());

                        dayPaint.setStyle(Paint.Style.FILL); //Modified code
                        dayPaint.setColor(event.getTextColor());  //Modified code
                        canvas.drawText(String.valueOf(dayOfMonth), xPosition, yPosition, dayPaint);  //Modified code
                    }
                } else {
                    yPosition += indicatorOffset;
                    // offset event indicators to draw below selected day indicators
                    // this makes sure that they do no overlap
                    if (shouldDrawIndicatorsBelowSelectedDays && (isSameDayAsCurrentDay || isCurrentSelectedDay)) {
                        yPosition += indicatorOffset;
                    }

                    if (eventsList.size() >= 3) {
                        drawEventsWithPlus(canvas, xPosition, yPosition, eventsList);
                    } else if (eventsList.size() == 2) {
                        drawTwoEvents(canvas, xPosition, yPosition, eventsList);
                    } else if (eventsList.size() == 1) {
                        drawSingleEvent(canvas, xPosition, yPosition, eventsList);
                    }
                }
            }
        }
    }
}`

` void drawMonth(Canvas canvas, Calendar monthToDrawCalender, int offset) { // drawEvents(canvas, monthToDrawCalender, offset);

    //offset by one because we want to start from Monday
    int firstDayOfMonth = getDayOfWeek(monthToDrawCalender);

    boolean isSameMonthAsToday = monthToDrawCalender.get(Calendar.MONTH) == todayCalender.get(Calendar.MONTH);
    boolean isSameYearAsToday = monthToDrawCalender.get(Calendar.YEAR) == todayCalender.get(Calendar.YEAR);
    boolean isSameMonthAsCurrentCalendar = monthToDrawCalender.get(Calendar.MONTH) == currentCalender.get(Calendar.MONTH) &&
                                           monthToDrawCalender.get(Calendar.YEAR) == currentCalender.get(Calendar.YEAR);
    int todayDayOfMonth = todayCalender.get(Calendar.DAY_OF_MONTH);
    boolean isAnimatingWithExpose = animationStatus == EXPOSE_CALENDAR_ANIMATION;

    int maximumMonthDay = monthToDrawCalender.getActualMaximum(Calendar.DAY_OF_MONTH);

    tempPreviousMonthCalendar.setTimeInMillis(monthToDrawCalender.getTimeInMillis());
    tempPreviousMonthCalendar.add(Calendar.MONTH, -1);
    int maximumPreviousMonthDay = tempPreviousMonthCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    for (int dayColumn = 0, colDirection = isRtl? 6 : 0, dayRow = 0; dayColumn <= 6; dayRow++) {
        if (dayRow == 7) {
            if (isRtl) {
                colDirection--;
            } else {
                colDirection++;
            }
            dayRow = 0;
            if (dayColumn <= 6) {
                dayColumn++;
            }
        }
        if (dayColumn == dayColumnNames.length) {
            break;
        }
        float xPosition = widthPerDay * dayColumn + paddingWidth + paddingLeft + accumulatedScrollOffset.x + offset - paddingRight;
        float yPosition = dayRow * heightPerDay + paddingHeight;
        if (xPosition >= growFactor && (isAnimatingWithExpose || animationStatus == ANIMATE_INDICATORS) || yPosition >= growFactor) {
            // don't draw days if animating expose or indicators
            continue;
        }
        if (dayRow == 0) {
            // first row, so draw the first letter of the day
            if (shouldDrawDaysHeader) {
                dayPaint.setColor(calenderTextColor);
                dayPaint.setTypeface(Typeface.DEFAULT_BOLD);
                dayPaint.setStyle(Paint.Style.FILL);
                dayPaint.setColor(calenderTextColor);
                canvas.drawText(dayColumnNames[colDirection], xPosition, paddingHeight, dayPaint);
                dayPaint.setTypeface(Typeface.DEFAULT);
            }
        } else {
            int day = ((dayRow - 1) * 7 + colDirection + 1) - firstDayOfMonth;
            int defaultCalenderTextColorToUse = calenderTextColor;
            if (currentCalender.get(Calendar.DAY_OF_MONTH) == day && isSameMonthAsCurrentCalendar && !isAnimatingWithExpose) {
                drawDayCircleIndicator(currentSelectedDayIndicatorStyle, canvas, xPosition, yPosition, currentSelectedDayBackgroundColor);
                defaultCalenderTextColorToUse = currentSelectedDayTextColor;
            } else if (isSameYearAsToday && isSameMonthAsToday && todayDayOfMonth == day && !isAnimatingWithExpose) {
                // TODO calculate position of circle in a more reliable way
                drawDayCircleIndicator(currentDayIndicatorStyle, canvas, xPosition, yPosition, currentDayBackgroundColor);
                defaultCalenderTextColorToUse = currentDayTextColor;
            }
            if (day <= 0) {
                if (displayOtherMonthDays) {
                    // Display day month before
                    dayPaint.setStyle(Paint.Style.FILL);
                    dayPaint.setColor(otherMonthDaysTextColor);
                    canvas.drawText(String.valueOf(maximumPreviousMonthDay + day), xPosition, yPosition, dayPaint);
                }
            } else if (day > maximumMonthDay) {
                if (displayOtherMonthDays) {
                    // Display day month after
                    dayPaint.setStyle(Paint.Style.FILL);
                    dayPaint.setColor(otherMonthDaysTextColor);
                    canvas.drawText(String.valueOf(day - maximumMonthDay), xPosition, yPosition, dayPaint);
                }
            } else {
                dayPaint.setStyle(Paint.Style.FILL);
                dayPaint.setColor(defaultCalenderTextColorToUse);
                canvas.drawText(String.valueOf(day), xPosition, yPosition, dayPaint);
            }
        }
    }

    drawEvents(canvas, monthToDrawCalender, offset);
}`

`public class Event {

private int color;
private int textColor;
private long timeInMillis;
private Object data;

public Event(int color, long timeInMillis) {
    this.color = color;
    this.timeInMillis = timeInMillis;
}

public Event(int color, long timeInMillis, Object data) {
    this.color = color;
    this.timeInMillis = timeInMillis;
    this.data = data;
}

public Event(int color, int textColor, long timeInMillis) {
    this.color = color;
    this.textColor = textColor;
    this.timeInMillis = timeInMillis;
}

public int getColor() {
    return color;
}

public long getTimeInMillis() {
    return timeInMillis;
}

@Nullable
public Object getData() {
    return data;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Event event = (Event) o;

    if (color != event.color) return false;
    if (timeInMillis != event.timeInMillis) return false;
    if (data != null ? !data.equals(event.data) : event.data != null) return false;

    return true;
}

@Override
public int hashCode() {
    int result = color;
    result = 31 * result + (int) (timeInMillis ^ (timeInMillis >>> 32));
    result = 31 * result + (data != null ? data.hashCode() : 0);
    return result;
}

public int getTextColor() {
    return textColor;
}

public void setTextColor(int textColor) {
    this.textColor = textColor;
}

@Override
public String toString() {
    return "Event{" +
            "color=" + color +
            ", timeInMillis=" + timeInMillis +
            ", data=" + data +
            '}';
}

} `