Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextField maxLength - Android Jetpack Compose

Is there any out of the box solution for limiting the character size in TextField's ? I don't see any maxLength parameter like we had in XML.

like image 940
Hassa Avatar asked Apr 17 '21 08:04

Hassa


People also ask

How do you change Text size in compose?

To change font size of Text composable in Android Jetpack Compose, pass a required font size value for the optional fontSize parameter of Text composable. Make sure to import sp , as shown in the above code snippet, when you are assign fontSize property with scale-independent pixels.

How do I make Text bold in compose?

Android Compose – Change Text to Bold To change font weight of Text composable to Bold, in Android Jetpack Compose, pass FontWeight. Bold for the optional fontWeight parameter of Text composable.


Video Answer


1 Answers

With 1.x.y there isn't a built-in parameter. You can use something like:

var text by remember { mutableStateOf(TextFieldValue("")) }
val maxChar = 5

TextField(
    singleLine = true,
    value = text,
    onValueChange = {
        if (it.length <= maxChar) text = it         
    }
)
like image 65
Gabriele Mariotti Avatar answered Oct 05 '22 15:10

Gabriele Mariotti