CleverPumpkin / CrunchyCalendar

A beautiful material calendar with endless scroll, range selection and a lot more!
MIT License
647 stars 45 forks source link

Disabled day cell with custom background color #24

Closed wyzard closed 5 years ago

wyzard commented 5 years ago

Thank you for this awesome library again. :)

Is there any way to set the 'disabled days' cell's background color conditionally? Example: one of them 'reserved' (red), another one is 'processing' (orange) stb..

Thank you.

rAseri commented 5 years ago

Hi and thank you!

Out of the box you can define your custom background color for disabled days using resources. Unfortunately conditional background color for disabled days is not supported.

But, as a workaround, you could try to define your custom RecyclerView.ItemDecoration class to decorate some specific days that you want. In the library there is a helper AbsDateItemDecoration class that may help you to add some custom decorations for the specific days. Create your item decorator that extends the AbsDateItemDecoration class. In the decorateDateView method you could implement your own custom decoration logic for the specific days.

Let me know if it helps you.

wyzard commented 5 years ago

Thank you for your answer. I found the addCustomItemDecoration method, but I had difficulties to preserve the original appearance (font size, text position etc. is perfect for me, the only thing that I want to change is the background color). Anyhow it seems to be good for me, thanks for your help.

wyzard commented 5 years ago

Sorry, it doesn't work for me. I can 'change' the background color of the cell, but I can't draw the day of month text on it. Do you have a working example maybe? This is my draft so far:

` private class CustomItemDecoration extends AbsDateItemDecoration {

    @Override
    public void decorateDateView(@NotNull Canvas canvas, @NotNull CalendarDate calendarDate, @NotNull Rect rect) {
        DisplayMetrics displayMetrics = ReservationActivity.this.getResources().getDisplayMetrics();
        int strokeWidth = Math.round(1f * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(Color.LTGRAY);
        paint.setStrokeWidth(strokeWidth);

        TextPaint paint2 = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        paint2.setColor(Color.RED);

        if (calendarDate.getDayOfMonth() == 14) {
            canvas.drawRect(rect.left + strokeWidth, rect.top + strokeWidth, rect.right - strokeWidth, rect.bottom - strokeWidth , paint);
            //canvas.drawText(String.valueOf(calendarDate.getDayOfMonth()), rect.width() / 2, rect.height() / 2, paint2);
            canvas.drawText("Test", rect.width() / 2f, rect.height() / 2f, paint2);
        }
    }
}

`

wyzard commented 5 years ago

Ok, my fault. It was a positioning problem. :)