Input: TextField, Checkbox, Switch and Radio Buttons
A State-Driven TextField
A TextField shows the text you give it. When the user types, you must update the state, which redraws the field.
kotlin
@Composable
fun NameField() {
var name by remember { mutableStateOf("") }
OutlinedTextField(
value = name,
onValueChange = { name = it },
label = { Text("Your name") },
leadingIcon = { Icon(Icons.Default.Person, contentDescription = null) },
singleLine = true,
modifier = Modifier.fillMaxWidth()
)
}
A Signup Form
kotlin
@Composable
fun SignupForm(onSubmit: (String, String) -> Unit) {
var email by rememberSaveable { mutableStateOf("") }
var password by rememberSaveable { mutableStateOf("") }
var showPassword by remember { mutableStateOf(false) }
var subscribe by rememberSaveable { mutableStateOf(true) }
val emailError = email.isNotEmpty() && !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
val valid = email.isNotEmpty() && !emailError && password.length >= 6
Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) {
OutlinedTextField(
value = email,
onValueChange = { email = it },
label = { Text("Email") },
isError = emailError,
supportingText = { if (emailError) Text("Enter a valid email") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email, imeAction = ImeAction.Next),
singleLine = true,
modifier = Modifier.fillMaxWidth()
)
OutlinedTextField(
value = password,
onValueChange = { password = it },
label = { Text("Password (6+ characters)") },
visualTransformation = if (showPassword) VisualTransformation.None else PasswordVisualTransformation(),
trailingIcon = {
IconButton(onClick = { showPassword = !showPassword }) {
Icon(
if (showPassword) Icons.Default.VisibilityOff else Icons.Default.Visibility,
contentDescription = if (showPassword) "Hide password" else "Show password"
)
}
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = ImeAction.Done),
singleLine = true,
modifier = Modifier.fillMaxWidth()
)
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(checked = subscribe, onCheckedChange = { subscribe = it })
Text("Send me news")
}
Button(
onClick = { onSubmit(email, password) },
enabled = valid,
modifier = Modifier.fillMaxWidth()
) { Text("Create account") }
}
}
rememberSaveable:
remember loses its value when the screen rotates. rememberSaveable survives rotation and process death for simple values.
Switch and Radio Buttons
kotlin
var notifications by remember { mutableStateOf(true) }
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically) {
Text("Notifications")
Switch(checked = notifications, onCheckedChange = { notifications = it })
}
val options = listOf("Small", "Medium", "Large")
var selected by remember { mutableStateOf(options[1]) }
Column(Modifier.selectableGroup()) {
options.forEach { option ->
Row(
Modifier.fillMaxWidth().selectable(selected = option == selected, onClick = { selected = option },
role = Role.RadioButton).padding(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(selected = option == selected, onClick = null)
Spacer(Modifier.width(8.dp))
Text(option)
}
}
}
Dropdown
kotlin
var expanded by remember { mutableStateOf(false) }
var country by remember { mutableStateOf("India") }
ExposedDropdownMenuBox(expanded = expanded, onExpandedChange = { expanded = it }) {
OutlinedTextField(
value = country, onValueChange = {}, readOnly = true, label = { Text("Country") },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded) },
modifier = Modifier.menuAnchor().fillMaxWidth()
)
ExposedDropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {
listOf("India", "USA", "UK").forEach {
DropdownMenuItem(text = { Text(it) }, onClick = { country = it; expanded = false })
}
}
}
Focus and Keyboard
kotlin
val focusManager = LocalFocusManager.current
OutlinedTextField(
value = text, onValueChange = { text = it },
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() })
)
Tip: Show validation errors only after the user typed something, so the form does not start in red.
Conclusion
Compose input widgets are state-driven. Always keep the text in state, set keyboard options and validate before enabling the submit button.
Quick Summary: TextField needs value and onValueChange. Use rememberSaveable, keyboardOptions, Checkbox, Switch, RadioButton and enabled buttons for validation.

