This is the code that i am using :
val picker = builder .setTitleText(getText(R.string.select_start_date_and_end_date)) .setSelection(Pair((selectedStartDate ?: calendar.timeInMillis), (selectedEndDate ?: calendar.timeInMillis))) .setTheme(AppUtils.resolveOrThrow(requireContext(), R.attr.materialCalendarTheme)) .setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR) .setCalendarConstraints(constraintsBuilder) .build() picker.show(parentFragmentManager, picker.toString())
While on click of positive button i am storing data as long value like this :
picker.addOnPositiveButtonClickListener { selectedStartDate = it.first ?: 0 selectedEndDate = it.second ?: 0 }
Here selectedStartDate and selectedEndDate are long variables. but calendar is not opening with previously selected dates.
Asked
Active
Viewed 698 times
1

Deepak Rajput
- 731
- 6
- 20
-
https://stackoverflow.com/questions/61661948/materialdatepicker-shows-current-date-instead-of-needed – Gabriele Mariotti May 28 '20 at 22:03
1 Answers
1
I found the answer.
val dateValidator: CalendarConstraints.DateValidator = DateValidatorPointForward.now()
val builder = MaterialDatePicker.Builder.dateRangePicker()
val constraintsBuilder =
CalendarConstraints.Builder().setStart(calendar.timeInMillis)
.setOpenAt((selectedStartDate ?: calendar.timeInMillis))
.setValidator(dateValidator).build()
val picker = builder
.setTitleText(getText(R.string.select_start_date_and_end_date))
.setSelection(
Pair(
(selectedStartDate ?: calendar.timeInMillis),
(selectedEndDate ?: calendar.timeInMillis)
)
)
.setTheme(AppUtils.resolveOrThrow(requireContext(), R.attr.materialCalendarTheme))
.setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR)
.setCalendarConstraints(constraintsBuilder)
.build()
picker.show(parentFragmentManager, picker.toString())
picker.addOnPositiveButtonClickListener {
selectedStartDate = it.first ?: 0
selectedEndDate = it.second ?: 0
setStartAndEndDate(selectedStartDate, selectedEndDate)
}
- There was a method openAt(). Thanks.

Deepak Rajput
- 731
- 6
- 20