TextField helps a mobile app collect user inputs. Usually, TextField has a single line but sometimes, you may need a multiline TextField to accommodate long-form texts. Let’s learn how to add a single line TextField in Jetpack Compose.
TextField composable is multiline by default. You can change it to a single-line TextField by making the singleLine parameter true. See the code snippet given below.
@Composable
fun TextFieldExample() {
var value by remember { mutableStateOf("") }
TextField(
value = value,
onValueChange = {
value = it
},
singleLine = true,
label = { Text("Enter text") },
modifier = Modifier.padding(20.dp)
)
}
Following is the output. Also, notice that the enter button on the keyboard changes to a tick mark.

Following is the complete code for reference.
package com.example.myapplication
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
Surface(
color = MaterialTheme.colors.background
) {
TextFieldExample()
}
}
}
}
}
@Composable
fun TextFieldExample() {
var value by remember { mutableStateOf("") }
TextField(
value = value,
onValueChange = {
value = it
},
singleLine = true,
label = { Text("Enter text") },
modifier = Modifier.padding(20.dp)
)
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
MyApplicationTheme {
TextFieldExample()
}
}
I hope you liked this Jetpack Compose single line TextField tutorial.