Search code examples
androidandroid-jetpack-composeandroid-compose-textfield

Android Compose TextField: How to set exact 3 lines


I want to create TextField with exact 3 lines:

destination

I want to see 3 lines even without any text in this TextField, i.e. I need a direct equivalent of EditText.lines in classic xml layout.

My not working code is:

OutlinedTextField(
            value = currentText,
            onValueChange = { currentText = it },
            label = { Text ("Label") },
            maxLines = 3,
            modifier = Modifier.fillMaxWidth().wrapContentHeight().padding(16.dp),
            singleLine = false
        )

Could you help me?


Solution

  • Starting from Starting from M2 1.4.0-alpha02 and M3 1.1.0-alpha02 you can use the minLines attribute in the TextField and OutlinedTextField:

    OutlinedTextField(
        value = text,
        onValueChange = { text = it },
        label = { Text ("Label") },
        minLines = 3,
        maxLines = 3,
        modifier = Modifier.fillMaxWidth().wrapContentHeight().padding(16.dp)
    )
    

    enter image description here enter image description here

    It can be used with M2 and M3.