Add text to dates in material-calendarview

Viewed 955

I'm using this material calendar view. It looks very rich in customizable options.

But there is no example of how to write text under day on certain dates.

I think that I can use DayViewDecorator but how to do this with short string? I tried with:

    calendarView.addDecorator(new DayViewDecorator() {
        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return true;
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.addSpan(new SpannableString("ABC"));
        }
    });

Without success.

1 Answers

@Thomas Banderas it's just like when you use dotspan class. suppose you want to add price for each date then use this code below.

class AddTextToDates(text: String) : LineBackgroundSpan{

    private var dayPrice = text

    override fun drawBackground(
        canvas: Canvas,
        paint: Paint,
        left: Int,
        right: Int,
        top: Int,
        baseline: Int,
        bottom: Int,
        text: CharSequence,
        start: Int,
        end: Int,
        lnum: Int
    ) {
        canvas.drawText(dayPrice,((left+right)/4).toFloat(),(bottom+15).toFloat(),paint)
    }
}

In your decorator class inside decorate, function add the above class as span here is the full class.

class YourDecorator(dates: List<CalendarDay>,priceText:String) :DayViewDecorator {
    private val dates: HashSet<CalendarDay> = HashSet(dates)
    var priceDay = priceText

    override fun shouldDecorate(day: CalendarDay): Boolean {
        return dates.contains(day)
    }

    override fun decorate(view: DayViewFacade) {
        view.addSpan(AddTextToDates(priceDay))
        view.setDaysDisabled(true)
    }

}

for working example browse this code and don't forget to star if it's useful for you. Hope it helps you guys :)

Related