I am using the prolificinteractive material-calendarview, and I also have schedules in the calendar. I already have 1 dot to represent that the day has a schedule on it.This is what my calendar looks like now What I want to know is how can I add multiple dots for different schedules that are on the same day?
This is where I set up my calendar
private fun setupCalendarView() {
mBinding.calendarView.setOnDateChangedListener { _, date, _ ->
val selectedDate = "${date.day}/${date.month + 1}/${date.year}"
showToast(selectedDate)
}
mMainViewModel.schedules.observe(this) { schedules ->
if (schedules != null) {
val occupiedDates = schedules.mapNotNull { stringToCalendarDay(it.date) }.toSet()
Log.d("Get Schedule", "Occupied Dates: $occupiedDates")
val decorator = OccupiedDateDecorator(occupiedDates)
mBinding.calendarView.removeDecorators() // Clear existing decorators
mBinding.calendarView.addDecorator(decorator)
mBinding.calendarView.invalidateDecorators() // Force refresh of decorators
}
}
}
private fun setupAddScheduleButton() {
mBinding.buttonAddSchedule.setOnClickListener {
val userId = getUserId()
val intent = Intent(this, AddScheduleActivity::class.java)
intent.putExtra("USER_ID", userId)
startActivity(intent)
}
}
private fun stringToCalendarDay(dateString: String): CalendarDay? {
return try {
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val date = sdf.parse(dateString)
val calendar = Calendar.getInstance().apply { time = date }
CalendarDay.from(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.DAY_OF_MONTH)
)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
My decorator for the calendar
class OccupiedDateDecorator(private val dates: Set<CalendarDay>) : DayViewDecorator {
override fun shouldDecorate(day: CalendarDay): Boolean {
return dates.contains(day)
}
override fun decorate(view: DayViewFacade) {
view.addSpan(DotSpan(5f, Color.RED)) // Adding a red dot below the date
}
}