Android MaterialDatePicker open on year selector

46 Views Asked by At

I would like to open the MaterialDatePicker to show the year selector on start before showing the actual calendar. I managed to display it by clicking the Year Button manually when the fragment is attached. But selecting a year makes the calendar scroll endlessly from January 1900 to current date. Adding a delay fixes the scrolling bug but it shows the calendar before navigating to the Year selector.enter image description here

val todayDate = MaterialDatePicker.todayInUtcMilliseconds()
val calendar = Calendar.getInstance()
calendar.timeInMillis = todayDate
calendar[Calendar.YEAR] -= minimumAge
val endDate = calendar.timeInMillis

val selectedDate = endDate

val constraints = CalendarConstraints.Builder()
    .setEnd(endDate)
    .setValidator(DateValidatorPointBackward.before(endDate))
    .build()

val datePicker = MaterialDatePicker
    .Builder
    .datePicker()
    .setTitleText(getString(R.string.date_picker_title_birthday))
    .setCalendarConstraints(constraints)
    .setSelection(selectedDate)
    .setTheme(R.style.MaterialCalendarTheme)
    .build()

fragmentManager.addFragmentOnAttachListener { _, fragment ->
    if (fragment.tag != tag) return@addFragmentOnAttachListener
    MainScope().launch {
        delay(1L)
        datePicker.view?.touchables?.find {
            it.id == com.google.android.material.R.id.month_navigation_fragment_toggle
        }?.performClick()
    }
}

datePicker.show(fragmentManager, tag)
0

There are 0 best solutions below