I have a textfield in android jetpack compose, so I want to put limit for numbers, for example; user can write only numbers from 1 to 10, is it possible to do it in jetpack compose?
@Preview(showBackground = true)
@Composable
fun OutlinedTextFieldComposable() {
var text by remember { mutableStateOf("") }
OutlinedTextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number))
}
You can make a condition for that inside onValueChange
like this:
@Preview(showBackground = true)
@Composable
fun OutlinedTextFieldComposable() {
var text by remember { mutableStateOf("") }
val maxNumbers = 10
OutlinedTextField(
value = text,
onValueChange = { if (it.toInt() <= maxNumbers) text = it },
label = { Text("Label") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With