TextField and User Input
Capture text from the user in Compose.
Capturing User Input
Almost every real app needs to ask the user for something: a name, an email, a search query. In Jetpack Compose, the building block for text entry is TextField.
In this lesson you'll learn how to display a text field, read what the user types, and keep the field in sync with your app's state. This is the foundation for every form you'll ever build.
Your First TextField
A TextField needs two things: a value (the current text to display) and an onValueChange callback (what to do when the user types).
Compose is declarative, so the field does not store its own text. You hold the text in state and feed it back in. This pattern is called state hoisting.
@Composable
fun NameField() {
var name by remember { mutableStateOf("") }
TextField(
value = name,
onValueChange = { name = it }
)
}All lessons in this course
- TextField and User Input
- Managing Form State
- Validating Input
- Keyboard Options and Actions