본문 바로가기
App Programming/Kotlin

[Kotlin] Decorator (Material CalendarView)

by goatlab 2023. 1. 19.
728x90
반응형
SMALL

Decorator

 

decorator 클래스를 선언하여 토요일, 일요일, 오늘에 대한 뷰 커스텀이 가능하다.

 

class main : AppCompatActivity() {
    val binding by lazy { ActivityMainBinding.inflate(layoutInflater)}
    lateinit var calendar: MaterialCalendarView
    private var Today = CalendarDay.today()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        val calendar = binding.calendarview
        calendar.setTitleFormatter(
            MonthArrayTitleFormatter(getResources().getTextArray(
            R.array.custom_months))
        );
        calendar.setWeekDayFormatter(ArrayWeekDayFormatter(getResources().getTextArray(R.array.custom_weekdays)));
        calendar.setSelectedDate(CalendarDay.today())

        val cal_dates = ArrayList<CalendarDay>()

        calendar.addDecorators(
            TodayDecorator(),
            SunDecorator(),
            SatDecorator())
    }
    
    class TodayDecorator: DayViewDecorator {
        private var date = CalendarDay.today()

        override fun shouldDecorate(day: CalendarDay?): Boolean {
            return day?.equals(date)!!
        }

        override fun decorate(view: DayViewFacade?) {
            view?.addSpan(StyleSpan(Typeface.BOLD))
            view?.addSpan(RelativeSizeSpan(1.4f))
            view?.addSpan(ForegroundColorSpan(Color.parseColor("#1D872A")))
        }
    }

    class SatDecorator: DayViewDecorator {
        private val calendar = Calendar.getInstance()

        override fun shouldDecorate(day: CalendarDay?): Boolean {
            day?.copyTo(calendar)
            val weekDay = calendar[Calendar.DAY_OF_WEEK]
            return weekDay == Calendar.SATURDAY
        }

        override fun decorate(view: DayViewFacade?) {
            view?.addSpan(ForegroundColorSpan(Color.BLUE))
        }
    }

    class SunDecorator: DayViewDecorator {
        private val calendar = Calendar.getInstance()

        override fun shouldDecorate(day: CalendarDay?): Boolean {
            day?.copyTo(calendar)
            val weekDay = calendar[Calendar.DAY_OF_WEEK]
            return weekDay == Calendar.SUNDAY
        }

        override fun decorate(view: DayViewFacade?) {
            view?.addSpan(ForegroundColorSpan(Color.RED))
        }
    }
}

728x90
반응형
LIST

'App Programming > Kotlin' 카테고리의 다른 글

[Kotlin] DotSpan (Material CalendarView)  (0) 2023.01.19
[Kotlin] Material CalendarView  (0) 2023.01.19
[Kotlin] timePicker  (0) 2023.01.17
[Kotlin] 구글 자동 로그인  (0) 2023.01.15
[Kotlin] 파이어베이스 구글 로그인  (0) 2023.01.15