DatePickerDialog
Android
Component in Material 3 Compose
A dialog for displaying a [DatePicker]. Date pickers let people select a date.
Last updated:
Installation
dependencies {
implementation("androidx.compose.material3:material3:1.4.0-alpha02")
}
Overloads
@ExperimentalMaterial3Api
@Composable
fun DatePickerDialog(
onDismissRequest: () -> Unit,
confirmButton: @Composable () -> Unit,
modifier: Modifier = Modifier,
dismissButton: @Composable (() -> Unit)? = null,
shape: Shape = DatePickerDefaults.shape,
tonalElevation: Dp = DatePickerDefaults.TonalElevation,
colors: DatePickerColors = DatePickerDefaults.colors(),
properties: DialogProperties = DialogProperties(usePlatformDefaultWidth = false),
content: @Composable ColumnScope.() -> Unit
)
Parameters
name | description |
---|---|
onDismissRequest | called when the user tries to dismiss the Dialog by clicking outside or pressing the back button. This is not called when the dismiss button is clicked. |
confirmButton | button which is meant to confirm a proposed action, thus resolving what triggered the dialog. The dialog does not set up any events for this button, nor does it control its enablement, so those need to be set up by the caller. |
modifier | the [Modifier] to be applied to this dialog's content. |
dismissButton | button which is meant to dismiss the dialog. The dialog does not set up any events for this button so they need to be set up by the caller. |
shape | defines the dialog's surface shape as well its shadow |
tonalElevation | when [DatePickerColors.containerColor] is [ColorScheme.surface], a higher the elevation will result in a darker color in light theme and lighter color in dark theme |
colors | [DatePickerColors] that will be used to resolve the colors used for this date picker in different states. See [DatePickerDefaults.colors]. |
properties | typically platform specific properties to further configure the dialog |
content | the content of the dialog (i.e. a [DatePicker], for example) |
Code Example
DatePickerDialogSample
@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun DatePickerDialogSample() {
// Decoupled snackbar host state from scaffold state for demo purposes.
val snackState = remember { SnackbarHostState() }
val snackScope = rememberCoroutineScope()
SnackbarHost(hostState = snackState, Modifier)
val openDialog = remember { mutableStateOf(true) }
// TODO demo how to read the selected date from the state.
if (openDialog.value) {
val datePickerState = rememberDatePickerState()
val confirmEnabled = remember {
derivedStateOf { datePickerState.selectedDateMillis != null }
}
DatePickerDialog(
onDismissRequest = {
// Dismiss the dialog when the user clicks outside the dialog or on the back
// button. If you want to disable that functionality, simply use an empty
// onDismissRequest.
openDialog.value = false
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
snackScope.launch {
snackState.showSnackbar(
"Selected date timestamp: ${datePickerState.selectedDateMillis}"
)
}
},
enabled = confirmEnabled.value
) {
Text("OK")
}
},
dismissButton = {
TextButton(onClick = { openDialog.value = false }) { Text("Cancel") }
}
) {
// The verticalScroll will allow scrolling to show the entire month in case there is not
// enough horizontal space (for example, when in landscape mode).
// Note that it's still currently recommended to use a DisplayMode.Input at the state in
// those cases.
DatePicker(
state = datePickerState,
modifier = Modifier.verticalScroll(rememberScrollState())
)
}
}
}