https://kotlinlang.org logo
#compose
Title
# compose
m

mertceyhan

02/21/2022, 2:01 PM
Hello all! I was working on a single-line text field and our QA engineer has noticed in the copy paste case the cursor goes to second-line though we set
singleLine = true
on
OutlinedTextField
. I’ve checked the case on EditText that comes from View class but it worked well. Is this bug or something else? Here is the code:
Copy code
Column(modifier = Modifier.padding(16.dp)) {
    var outlinedTextFieldValue by remember { mutableStateOf("") }
    OutlinedTextField(
        value = outlinedTextFieldValue,
        onValueChange = { newValue ->
            outlinedTextFieldValue = newValue
        },
        placeholder = { Text(text = "OutlinedTextField") },
        singleLine = true,
        maxLines = 1
    )


    AndroidView(factory = { context ->
        EditText(context).apply {
            hint = "EditText build by AndroidView"
            maxLines = 1
            isSingleLine = true
        }
    })
}
As a workaround solution I’ve replaced all
"\n"
with a space
" "
Copy code
var outlinedTextFieldValue by remember { mutableStateOf("") }
OutlinedTextField(
    value = outlinedTextFieldValue,
    onValueChange = { newValue ->
        outlinedTextFieldValue = newValue.replace("\n", " ")
    },
    placeholder = { Text(text = "OutlinedTextField") },
    singleLine = true,
    maxLines = 1
)
👍 2
s

Siyamed

02/21/2022, 8:32 PM
It is the expected solution, compose doesn't filter line/paragraph breaks
👍 1
The definition of singleline in material/basic should be checked though. Please create a ticket for material components as the foundation BasicTextField doesn't have the concept of singleline
Also what you get afaik can be any of the following characters (especially with copy paste)
m

mertceyhan

02/21/2022, 10:41 PM
Also we’ve noticed there is an issue with manipulating the new value in
onValueChange
. https://blog.shreyaspatil.dev/filtering-and-modifying-text-input-in-jetpack-compose-way. We’ve written a
SingleLineVisualTransformation
and it works fine right now. But we weren’t expecting that even though
maxLines = 1
. I’ll create a ticket and I’ll consider the different line breaks in char. Thanks a lot Siyamed
I’ve checked
BasicTextField
and it has
maxLines
and
singleLine
parameters but it works well on the copy-paste case. The implementation of it is different than
OutlinedTextField
. https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/package-summary#BasicTextField(kotlin.St[…]s.Brush,kotlin.Function1)
s

Siyamed

02/21/2022, 11:03 PM
thanks. I probably misread BasicText instead of BasicTextField in the docs when I checked.
👍 1
16 Views