Creates a [DatePickerState].
DatePickerCustomLocaleApi26Sample
@RequiresApi(Build.VERSION_CODES.O)
@Preview
@Composable
fun DatePickerCustomLocaleApi26Sample() {
val preferredLocales = LocaleList.forLanguageTags("HE")
val config = Configuration()
config.setLocales(preferredLocales)
val newContext = LocalContext.current.createConfigurationContext(config)
CompositionLocalProvider(
LocalContext provides newContext,
LocalConfiguration provides config,
LocalLayoutDirection provides LayoutDirection.Rtl,
) {
Column(
modifier = Modifier.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
// Pre-select a date for January 4, 2020
// Initialize date picker with the preferred locale. Here we create a state directly,
// but since the Locale was set at the CompositionLocalProvider through a Configuration,
// a `val datePickerState = rememberDatePickerState(...)` will have the same effect.
val datePickerState = remember {
DatePickerState(
initialSelectedDate = LocalDate.of(2020, 1, 4),
// Set to "HE" locale.
locale = preferredLocales.get(0),
)
}
DatePicker(state = datePickerState, modifier = Modifier.padding(16.dp))
Text(
"Selected date: ${datePickerState.getSelectedDate() ?: "no selection"}",
modifier = Modifier.align(Alignment.CenterHorizontally),
)
}
}
}
DatePickerCustomLocaleSample
@RequiresApi(Build.VERSION_CODES.N)
@Preview
@Composable
fun DatePickerCustomLocaleSample() {
val preferredLocales = LocaleList.forLanguageTags("HE")
val config = Configuration()
config.setLocales(preferredLocales)
val newContext = LocalContext.current.createConfigurationContext(config)
CompositionLocalProvider(
LocalContext provides newContext,
LocalConfiguration provides config,
LocalLayoutDirection provides LayoutDirection.Rtl,
) {
Column(
modifier = Modifier.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
// Pre-select a date for January 4, 2020
// Initialize date picker with the preferred locale. Here we create a state directly,
// but since the Locale was set at the CompositionLocalProvider through a Configuration,
// a `val datePickerState = rememberDatePickerState(...)` will have the same effect.
val datePickerState = remember {
DatePickerState(
initialSelectedDateMillis = 1578096000000,
// Set to "HE" locale.
locale = preferredLocales.get(0),
)
}
DatePicker(state = datePickerState, modifier = Modifier.padding(16.dp))
Text(
"Selected date timestamp: ${datePickerState.selectedDateMillis ?: "no selection"}",
modifier = Modifier.align(Alignment.CenterHorizontally),
)
}
}
}