Last active
June 28, 2024 16:46
-
-
Save crisu83/92705fde7e2f27599a71642704e0ce62 to your computer and use it in GitHub Desktop.
DatePicker and TimePicker components for Jetpack Compose.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun DatePicker( | |
label: String, | |
value: String, | |
onValueChange: (String) -> Unit = {}, | |
keyboardActions: KeyboardActions = KeyboardActions.Default, | |
keyboardOptions: KeyboardOptions = KeyboardOptions.Default, | |
pattern: String = "yyyy-MM-dd", | |
) { | |
val formatter = DateTimeFormatter.ofPattern(pattern) | |
val date = if (value.isNotBlank()) LocalDate.parse(value, formatter) else LocalDate.now() | |
val dialog = DatePickerDialog( | |
LocalContext.current, | |
{ _, year, month, dayOfMonth -> | |
onValueChange(LocalDate.of(year, month + 1, dayOfMonth).toString()) | |
}, | |
date.year, | |
date.monthValue - 1, | |
date.dayOfMonth, | |
) | |
TextField( | |
label = { Text(label) }, | |
value = value, | |
onValueChange = onValueChange, | |
enabled = false, | |
modifier = Modifier.clickable { dialog.show() }, | |
keyboardOptions = keyboardOptions, | |
keyboardActions = keyboardActions, | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun TimePicker( | |
label: String, | |
value: String, | |
onValueChange: (String) -> Unit, | |
keyboardActions: KeyboardActions = KeyboardActions.Default, | |
keyboardOptions: KeyboardOptions = KeyboardOptions.Default, | |
pattern: String = "HH:mm", | |
is24HourView: Boolean = true, | |
) { | |
val formatter = DateTimeFormatter.ofPattern(pattern) | |
val time = if (value.isNotBlank()) LocalTime.parse(value, formatter) else LocalTime.now() | |
val dialog = TimePickerDialog( | |
LocalContext.current, | |
{ _, hour, minute -> onValueChange(LocalTime.of(hour, minute).toString()) }, | |
time.hour, | |
time.minute, | |
is24HourView, | |
) | |
TextField( | |
label = { Text(label) }, | |
value = value, | |
onValueChange = onValueChange, | |
enabled = false, | |
modifier = Modifier.clickable { dialog.show() }, | |
keyboardOptions = keyboardOptions, | |
keyboardActions = keyboardActions, | |
) | |
} |
@saliouseck2009 You're welcome. I'm glad that you found this useful!
I have updated the Gist to fix the issue with TextField
components label
property.
TextField
needs both value
and onValueChange
parameters to compile. TextField
should be:
TextField(
label = { Text(label) },
value = value,
onValueChange = onValueChange,
enabled = false,
modifier = Modifier.clickable { dialog.show() },
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions
)
I have again updated the Gist as instructed but I'm unable to test that it actually works so I cannot guarantee that the components works with the latest version Jetpack Compose.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank's very much for sharing .
Just a little issues : The TextField label accepte now composable so we should have .
label = { Text(label) },