Skip to content

Instantly share code, notes, and snippets.

@ozgurg
Last active April 21, 2023 22:53
Show Gist options
  • Save ozgurg/fe8b3c28ce5435868539a884b981c2bc to your computer and use it in GitHub Desktop.
Save ozgurg/fe8b3c28ce5435868539a884b981c2bc to your computer and use it in GitHub Desktop.
Android DateTimePicker (MaterialDatePicker + MaterialTimePicker)

Android DateTimePicker (MaterialDatePicker + MaterialTimePicker)

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.

Usage

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
}
import androidx.fragment.app.FragmentManager
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.timepicker.MaterialTimePicker
import java.util.Calendar
class DateTimePicker(
private val datePicker: MaterialDatePicker<Long>,
private val timePicker: MaterialTimePicker
) {
private var onDateTimeSelectedListener: ((dateTime: DateTime) -> Unit)? = null
data class DateTime(
val year: String,
val monthOfYear: String,
val dayOfMonth: String,
var hourOfDay: String,
val minuteOfHour: String,
var calendar: Calendar
)
fun show(fragmentManager: FragmentManager, tag: String) {
datePicker.show(fragmentManager, "${tag}_DATE_PICKER")
datePicker.addOnPositiveButtonClickListener { selectionInMillis ->
timePicker.show(fragmentManager, "${tag}_TIME_PICKER")
timePicker.addOnPositiveButtonClickListener {
val calendar = Calendar.getInstance()
calendar.timeInMillis = selectionInMillis
calendar.set(Calendar.HOUR_OF_DAY, timePicker.hour)
calendar.set(Calendar.MINUTE, timePicker.minute)
onDateTimeSelectedListener?.invoke(calendarToDateTime(calendar))
}
}
}
fun setOnDateTimeSelectedListener(listener: (dateTime: DateTime) -> Unit) {
onDateTimeSelectedListener = listener
}
private fun calendarToDateTime(calendar: Calendar): DateTime {
return DateTime(
year = leadZero(calendar.get(Calendar.YEAR), 4),
monthOfYear = leadZero(calendar.get(Calendar.MONTH)),
dayOfMonth = leadZero(calendar.get(Calendar.DAY_OF_MONTH)),
hourOfDay = leadZero(calendar.get(Calendar.HOUR_OF_DAY)),
minuteOfHour = leadZero(calendar.get(Calendar.MINUTE)),
calendar = calendar
)
}
private fun leadZero(value: Int, zeroCount: Int = 2): String {
return String.format("%0${zeroCount}d", value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment