20

Is it possible to set singleLine or maxLines on TextField?

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

3 Answers 3

29

You can use the parameter maxLines or singleLine:

TextField(
    //..
    maxLines = 1)

or

TextField(
    //..
    singleLine = true)
Sign up to request clarification or add additional context in comments.

5 Comments

Is there any difference between those two? To me it looks like singleLine is redundant.
I assuming the difference in Compose is the same as that of xml. See stackoverflow.com/questions/30879471/…
The documentation states that when singleLine is set to true, maxLines will automatically be set to 1. However, neither setting stops the input to accept line breaks (e.g. from an external or keyboard that does not honor the imeAction).
There is a difference between singleLine and maxLines=1. singleLine = true forces text to be scrollable. Max lines is not.
Additionally if you set maxLines=1 you still can write a new line sign by clicking 'enter' on e.x. Gboard. But if you instead set singleLine = true - 'enter' key would close the keyboard and doen't affect the text.
10

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
)

Comments

-3

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
        }
    }
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.