Here I have a DateRangePicker
using the Material library.
val dateRangePicker =
MaterialDatePicker.Builder.dateRangePicker()
.setTitleText(getString(R.string.calendar_select_dates))
.setSelection(...)
.build()
How can I set minDate and maxDate to show me the only range between minDate<...<maxDate? In the documentation, it's mentioned how to disable the previous month but I want to disable the previous and following days.
Here is the sample code to set the min and max dates for restricting the date picker between two dates.
private fun showDatePickerDialog() {
val constraintsBuilder = CalendarConstraints.Builder()
// Set the minimum selectable date to a specific date (e.g., January 1, 2000).
val calendar = Calendar.getInstance()
val min = calendar.timeInMillis
constraintsBuilder.setStart(min)
calendar.add(Calendar.DAY_OF_MONTH, 10)
val max = calendar.timeInMillis
constraintsBuilder.setEnd(max)
val listValidators =
arrayListOf(DateValidatorPointForward.from(min), DateValidatorPointBackward.before(max))
constraintsBuilder.setValidator(CompositeDateValidator.allOf(listValidators))
val datePickerBuilder = MaterialDatePicker.Builder.datePicker().apply {
setTitleText(getString(R.string.app_name))
setCalendarConstraints(constraintsBuilder.build())
// Set the initial date to the current date
val currentTimeMillis = Calendar.getInstance().timeInMillis
setSelection(currentTimeMillis)
setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR)
}
val datePicker = datePickerBuilder.build()
datePicker.addOnPositiveButtonClickListener { selection ->
// Get selected date
}
// Don't forget to show the dialog
datePicker.show(supportFragmentManager, "datePicker")
}