Compose Component

TextField

Material Design filled text field.

TextField social preview

SimpleTextFieldSample

@Sampled
@Composable
fun SimpleTextFieldSample() {
    TextField(
        state = rememberTextFieldState(),
        label = { Text("Label") },
        lineLimits = TextFieldLineLimits.SingleLine,
    )
}

TextFieldWithInitialValueAndSelection

@Sampled
@Composable
fun TextFieldWithInitialValueAndSelection() {
    val state = rememberTextFieldState("Initial text", TextRange(0, 12))
    TextField(state = state, label = { Text("Label") }, lineLimits = TextFieldLineLimits.SingleLine)
}

TextFieldWithPlaceholder

@Sampled
@Composable
fun TextFieldWithPlaceholder() {
    TextField(
        state = rememberTextFieldState(),
        lineLimits = TextFieldLineLimits.SingleLine,
        label = { Text("Email") },
        placeholder = { Text("[email protected]") },
    )
}

TextFieldWithIcons

@Sampled
@Composable
fun TextFieldWithIcons() {
    val state = rememberTextFieldState()
    TextField(
        state = state,
        lineLimits = TextFieldLineLimits.SingleLine,
        placeholder = { Text("placeholder") },
        leadingIcon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
        trailingIcon = {
            IconButton(onClick = { state.clearText() }) {
                Icon(Icons.Filled.Clear, contentDescription = "Clear text")
            }
        },
    )
}

TextFieldWithErrorState

@Sampled
@Composable
fun TextFieldWithErrorState() {
    val state = rememberTextFieldState()
    var isError by rememberSaveable { mutableStateOf(false) }

TextFieldWithHelperMessage

@Sampled
@Composable
fun TextFieldWithHelperMessage() {
    Column {
        TextField(
            state = rememberTextFieldState(),
            label = { Text("Label") },
            lineLimits = TextFieldLineLimits.SingleLine,
        )
        Text(
            text = "Helper message",
            color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.medium),
            style = MaterialTheme.typography.caption,
            modifier = Modifier.padding(start = 16.dp),
        )
    }
}

TextFieldWithHideKeyboardOnImeAction

@Sampled
@Composable
fun TextFieldWithHideKeyboardOnImeAction() {
    val keyboardController = LocalSoftwareKeyboardController.current
    TextField(
        state = rememberTextFieldState(),
        label = { Text("Label") },
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
        onKeyboardAction = { keyboardController?.hide() },
    )
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: