MaterialDatePicker 의 setCalendarConstraints() 메소드
MaterialDatePicker.Builder.datePicker().setCalendarConstraints([CalendarConstraints])
CalendarConstraints 의 setValidator() 메소드를 사용한다.
CalendarConstrains.Builder().setValidator([DateValidator]).build()
DateValidatorPointBackward
CalendarConstraints.Builder()
.setValidator(DateValidatorPointBackward.now()).build()
DateValidatorPointForward
CalendarConstraints.Builder()
.setValidator(DateValidatorPointForward.now()).build()
달력의 최대값을(month 단위) 설정해 달력이 다음 달로 넘어가지 않도록 해준다.
CalendarConstraints.Builder.setEnd()
val calendar = Calendar.getInstance()
val presentMonth = calendar.timeInMillis
CalendarConstraints.Builder().setEnd(presentMonth)
달력의 최소값을(month 단위) 설정해 달력이 이전 달로 넘어가지 않도록 해준다.
CalendarConstraints.Builder.setStart()
val calendar = Calendar.getInstance()
val presentMonth = calendar.timeInMillis
CalendarConstraints.Builder().setStart(presentMonth)
private fun showDatePicker(
activity : AppCompatActivity,
date: LocalDateTime?,
isBirthDay: Boolean,
updatedDate: (LocalDateTime) -> Unit)
{
val selection = date?.atZone(ZoneId.of("UTC"))
val selectionMs = selection?.toInstant()?.toEpochMilli()
val calendar = Calendar.getInstance()
val presentMonth = calendar.timeInMillis
val upTo = calendar.timeInMillis.plus(172800000) //min date 설정. 오늘포함 3일 이후로 날짜 선택 가능
val constraints: CalendarConstraints = if (isBirthDay) {
// set max month and date
CalendarConstraints.Builder().setEnd(presentMonth)
.setValidator(DateValidatorPointBackward.now()).build()
} else {
// set min month and date
CalendarConstraints.Builder().setStart(presentMonth)
.setValidator(DateValidatorPointForward.from(upTo)).build()
}
val picker = MaterialDatePicker.Builder.datePicker()
.setCalendarConstraints(constraints)
.setSelection(selectionMs)
.build()
picker.show(activity.supportFragmentManager, "picker")
picker.addOnPositiveButtonClickListener {
val date = LocalDateTime.ofInstant(
Instant.ofEpochMilli(it), ZoneId.of("UTC")
)
updatedDate(date)
}
}