Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put the limit for numbers in textfield for android jetpack compose?

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))
 
}
like image 465
Huma33 Avatar asked Sep 05 '25 03:09

Huma33


1 Answers

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))
 
}
like image 100
Mohamed Rejeb Avatar answered Sep 08 '25 10:09

Mohamed Rejeb