{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

DatePickerDialogKt

public final class DatePickerDialogKt


Summary

Public methods

static final void
@ExperimentalMaterial3Api
@Composable
DatePickerDialog(
    @NonNull Function0<Unit> onDismissRequest,
    @Composable @NonNull Function0<Unit> confirmButton,
    @NonNull Modifier modifier,
    @Composable Function0<Unit> dismissButton,
    @NonNull Shape shape,
    @NonNull Dp tonalElevation,
    @NonNull DatePickerColors colors,
    @NonNull DialogProperties properties,
    @Composable @ExtensionFunctionType @NonNull Function1<@NonNull ColumnScopeUnit> content
)

Material Design date picker dialog.

Public methods

DatePickerDialog

@ExperimentalMaterial3Api
@Composable
public static final void DatePickerDialog(
    @NonNull Function0<Unit> onDismissRequest,
    @Composable @NonNull Function0<Unit> confirmButton,
    @NonNull Modifier modifier,
    @Composable Function0<Unit> dismissButton,
    @NonNull Shape shape,
    @NonNull Dp tonalElevation,
    @NonNull DatePickerColors colors,
    @NonNull DialogProperties properties,
    @Composable @ExtensionFunctionType @NonNull Function1<@NonNull ColumnScopeUnit> content
)

Material Design date picker dialog.

A dialog for displaying a DatePicker. Date pickers let people select a date.

A sample for displaying a DatePicker in a dialog:

import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier

// 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")
            }
        }
    ) {
        DatePicker(state = datePickerState)
    }
}
Parameters
@NonNull Function0<Unit> 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.

@Composable @NonNull Function0<Unit> 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.

@NonNull Modifier modifier

the Modifier to be applied to this dialog's content.

@Composable Function0<Unit> 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.

@NonNull Shape shape

defines the dialog's surface shape as well its shadow

@NonNull Dp 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

@NonNull DatePickerColors colors

DatePickerColors that will be used to resolve the colors used for this date picker in different states. See DatePickerDefaults.colors.

@NonNull DialogProperties properties

typically platform specific properties to further configure the dialog

@Composable @ExtensionFunctionType @NonNull Function1<@NonNull ColumnScopeUnit> content

the content of the dialog (i.e. a DatePicker, for example)