A highly customizable date picker for Jetpack Compose. Material3 theme-compatible.
Minimal working example of a datepicker dialog usage (see below for parameter descriptions):
var isDialogShown: Boolean by rememberSaveable {
mutableStateOf(false)
}
var date: LocalDate? by remember {
mutableStateOf(null)
}
if (isDialogShown) {
DatePickerDialog(
onDismissRequest = { isDialogShown = false },
onDateChange = {
date = it
isDialogShown = false
},
// Optional but recommended parameter to provide the title for the dialog
title = { Text(text = "Select date") }
)
}
DatePickerDialog
with all parameters:
@Composable
fun DatePickerDialog(
onDismissRequest: () -> Unit,
onDateChange: (LocalDate) -> Unit,
modifier: Modifier = Modifier,
initialDate: LocalDate? = null,
locale: Locale = LocalConfiguration.current.getDefaultLocale(),
today: LocalDate = LocalDate.now(),
showDaysAbbreviations: Boolean = true,
highlightToday: Boolean = true,
colors: DatePickerColors = DatePickerDefaults.colors(),
shapes: DatePickerShapes = DatePickerDefaults.shapes(),
typography: DatePickerTypography = DatePickerDefaults.typography(),
title: @Composable (() -> Unit)? = null,
buttonColors: ButtonColors = ButtonDefaults.textButtonColors(),
shape: Shape = AlertDialogDefaults.shape,
containerColor: Color = AlertDialogDefaults.containerColor,
titleContentColor: Color = AlertDialogDefaults.titleContentColor,
tonalElevation: Dp = AlertDialogDefaults.TonalElevation,
properties: DialogProperties = DialogProperties(usePlatformDefaultWidth = false),
)
onDismissRequest
- called when the dialog should be dismissed without user selecting a valueonDateChange
- called when user selected a value, passing it as a parametermodifier
- aModifier
for the root@Composable
initialDate
- an initially-selectedLocalDate
ornull
locale
-java.util.Locale
used to display user-visible strings, such as names of days and monthstoday
-LocalDate
representing todayshowDaysAbbreviations
- whether or not show row with days abbreviations above the day gridhighlightToday
- whether or not highlight today in the day gridcolors
- an instance ofDatePickerColors
used to theme the component (see below for more info)shapes
- an instance ofDatePickerShapes
used to theme the component (see below for more info)typography
- an instance ofDatePickerTypography
used to theme the component (see below for more info)title
- a@Composable
slot for the dialog title - usually{ Text("Select date") }
or similarbuttonColors
- an instance of Material'sButtonColors
to customize the appearance of dialog buttonsshape
- the shape of theAlertDialog
containerColor
- the container color of theAlertDialog
tonalElevation
- the tonal elevation of theAlertDialog
properties
-DialogProperties
of theAlertDialog
Datepicker dialog provides several ways of customizing its looks. From small details controlled by showDaysAbbreviations
and highlightToday
to the more complex combination of different Color
, Shape
and Typography
.
Datepicker dialog offers out-of-the-box support for light/dark theming and Material You dynamic colors, as long as your MaterialTheme
is defined correctly.
The components use the design tokens that reference attributes from the MaterialTheme
.
For example, passing
DatePickerDialog(
// ...
shapes = DatePickerDefaults.shapes(
currentMonthDaySelected = CutCornerShape(percent = 40),
currentMonthDayToday = RoundedCornerShape(4.dp),
),
)
produces
In the similar way you can customize colors
, typography
, and even the looks of the AlertDialog
itself.
For default values see DatePickerDefaults.
Newly, there is a possibility to alter dialog buttons' colors using the buttonColors
parameter. Simply pass an instance of ButtonColors
:
DatePickerDialog(
//...,
buttonColors = ButtonDefaults.textButtonColors(
contentColor = yourDesiredColor,
),
//...,
)