Skip to content

Instantly share code, notes, and snippets.

@crisu83
Last active February 12, 2024 10:22
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisu83/92705fde7e2f27599a71642704e0ce62 to your computer and use it in GitHub Desktop.
Save crisu83/92705fde7e2f27599a71642704e0ce62 to your computer and use it in GitHub Desktop.
DatePicker and TimePicker components for Jetpack Compose.
@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,
)
}
@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
Copy link

Thank's very much for sharing .
Just a little issues : The TextField label accepte now composable so we should have .
label = { Text(label) },

@crisu83
Copy link
Author

crisu83 commented Feb 7, 2024

@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.

@joshgalvan
Copy link

joshgalvan commented Feb 9, 2024

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
)

@crisu83
Copy link
Author

crisu83 commented Feb 12, 2024

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