The DateTimePicker class combines the MaterialDatePicker and MaterialTimePicker provided by the AndroidX library to create a picker for both date and time. This class simplifies the creation of a DateTimePicker by handling the interaction between both pickers and providing a structured DateTime object via a callback when the user selects a date and time.
When the show()
method is called with a FragmentManager and tag, the DateTimePicker displays the MaterialDatePicker and
the MaterialTimePicker one after the other. When the user selects a date and time, the onDateTimeSelectedListener
callback
is called with a DateTime object that contains the selected year, month, day, hour, minute, and a Calendar object
representing the selected date and time.
val datePicker = MaterialDatePicker.Builder.datePicker()
.build()
val timePicker = MaterialTimePicker.Builder()
.build()
val dateTimePicker = DateTimePicker(datePicker, timePicker)
dateTimePicker.show(parentFragmentManager, "DATE_TIME_PICKER_TAG")
dateTimePicker.setOnDateTimeSelectedListener { dateTime ->
// Do whatever you want with dateTime
}