Jetpack Compose SingleLine Input Text

Viewed 5677

Is it possible to set singleLine or maxLines on TextField?

I've checked a source and it's missing. Any ideas / workarounds?

3 Answers

You can use the parameter maxLines or singleLine:

TextField(
    //..
    maxLines = 1)

or

TextField(
    //..
    singleLine = true)

Since Compose 1.0.0-alpha08, you can use singleLine parameter to make the text field a single horizontally scrollable line:

TextField(
    value = text,
    onValueChange = { },
    singleLine = true
)

I can't see any property which can do it directly. One work around could be:

TextField(
    value = yourText,
    onValueChange = { s: TextFieldValue ->
        if (s.text.count { it == '\n' } < 3) { // 3 lines (or two enters)
            yourText = s
        }
    }
)
Related