androidx.compose.material3

TextField

Composable function

A text field lets the user enter and edit text. There are two looks: filled (TextField) and outlined (OutlinedTextField). They take the same parameters.

A text field with a label
A text field with a label
val name = rememberTextFieldState("Ada")
TextField(state = name, label = { Text("Name") })
Filled and outlined
Filled and outlined
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
    TextField(state = rememberTextFieldState(), label = { Text("Filled") })
    OutlinedTextField(state = rememberTextFieldState(), label = { Text("Outlined") })
}

For passwords, use SecureTextField or OutlinedSecureTextField. They hide the text and stop copying.

A password field
A password field
OutlinedSecureTextField(
    state = rememberTextFieldState("hunter2"),
    label = { Text("Password") },
)

There are three forms. Use the first one, with TextFieldState, for new code. Create the state with rememberTextFieldState().

StateSince 1.9

The recommended form. Takes a TextFieldState that holds the text.

@Composable
fun TextField(
    state: TextFieldState,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    textStyle: TextStyle = LocalTextStyle.current,
    labelPosition: TextFieldLabelPosition = TextFieldLabelPosition.Inside(),
    label: @Composable (TextFieldLabelScope.() -> Unit)? = null,
    placeholder: @Composable (() -> Unit)? = null,
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    prefix: @Composable (() -> Unit)? = null,
    suffix: @Composable (() -> Unit)? = null,
    supportingText: @Composable (() -> Unit)? = null,
    isError: Boolean = false,
    inputTransformation: InputTransformation? = null,
    outputTransformation: OutputTransformation? = null,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    onKeyboardAction: KeyboardActionHandler? = null,
    lineLimits: TextFieldLineLimits = TextFieldLineLimits.Default,
    onTextLayout: (Density.(getResult: () -> TextLayoutResult?) -> Unit)? = null,
    scrollState: ScrollState = rememberScrollState(),
    shape: Shape = TextFieldDefaults.shape,
    colors: TextFieldColors = TextFieldDefaults.colors(),
    contentPadding: PaddingValues = TextFieldDefaults.defaultContentPadding(label, labelPosition),
    interactionSource: MutableInteractionSource? = null,
)

state: TextFieldStateRequired

Holds the text and the cursor. Create it with rememberTextFieldState("initial text") and read it with state.text.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position of the field. See the Modifier guide. Use Modifier.fillMaxWidth() to stretch it.

enabled: BooleanOptional

Default: true

Set to false to make the field gray. The user cannot focus, edit or copy it.

readOnly: BooleanOptional

Default: false

Set to true to block editing. The user can still focus, select and copy the text.

textStyle: TextStyleOptional

Default: LocalTextStyle.current

The style of the entered text. Default: the current LocalTextStyle.

labelPosition: TextFieldLabelPositionOptional

Default: TextFieldLabelPosition.Inside()

Where the label sits: inside the field (default for TextField), cut into the outline (default for OutlinedTextField), or above it.

label: @Composable (TextFieldLabelScope.() -> Unit)?Optional

Default: null

A short name for the field, like "Email". It sits inside the field and moves up when the field has text or focus.

placeholder: @Composable (() -> Unit)?Optional

Default: null

Gray text shown while the field is empty and focused, like an example value.

Placeholder text
Placeholder text
OutlinedTextField(
    state = rememberTextFieldState(),
    placeholder = { Text("you@example.com") },
    label = { Text("Email") },
)

leadingIcon: @Composable (() -> Unit)?Optional

Default: null

An icon at the start of the field, like a search icon.

Leading and trailing icons
Leading and trailing icons
val query = rememberTextFieldState("Lisbon")
TextField(
    state = query,
    leadingIcon = { Icon(Icons.Filled.Search, contentDescription = null) },
    trailingIcon = { Icon(Icons.Filled.Clear, contentDescription = "Clear") },
    lineLimits = TextFieldLineLimits.SingleLine,
)

trailingIcon: @Composable (() -> Unit)?Optional

Default: null

An icon at the end of the field, like a clear button or a password toggle.

prefix: @Composable (() -> Unit)?Optional

Default: null

Text before the input that the user cannot edit, like a currency symbol.

Prefix and suffix
Prefix and suffix
OutlinedTextField(
    state = rememberTextFieldState("25"),
    label = { Text("Price") },
    prefix = { Text("€") },
    suffix = { Text("per night") },
    modifier = Modifier.width(240.dp),
)

suffix: @Composable (() -> Unit)?Optional

Default: null

Text after the input that the user cannot edit, like a unit.

supportingText: @Composable (() -> Unit)?Optional

Default: null

A line of help or error text below the field.

isError: BooleanOptional

Default: false

Set to true to show the field in the error color. Explain the error in supportingText.

An error with supporting text
An error with supporting text
OutlinedTextField(
    state = rememberTextFieldState("12"),
    label = { Text("Age") },
    isError = true,
    supportingText = { Text("Must be 18 or older") },
)

inputTransformation: InputTransformation?Optional

Default: null

Changes or rejects what the user types, for example to allow only digits.

outputTransformation: OutputTransformation?Optional

Default: null

Changes how the text is shown without changing the text itself, for example to add spaces to a phone number.

keyboardOptions: KeyboardOptionsOptional

Default: KeyboardOptions.Default

Which keyboard to show (text, number, email, …) and what the action key says (Done, Search, Next, …).

onKeyboardAction: KeyboardActionHandler?Optional

Default: null

Called when the user presses the keyboard action key.

lineLimits: TextFieldLineLimitsOptional

Default: TextFieldLineLimits.Default

One line or several. TextFieldLineLimits.SingleLine stops Enter from adding a new line.

Several lines
Several lines
OutlinedTextField(
    state = rememberTextFieldState("Line one\nLine two"),
    label = { Text("Notes") },
    lineLimits = TextFieldLineLimits.MultiLine(minHeightInLines = 3),
    modifier = Modifier.width(280.dp),
)

onTextLayout: (Density.(getResult: () -> TextLayoutResult?) -> Unit)?Optional

Default: null

Called with the text layout after it changes. Rarely needed.

scrollState: ScrollStateOptional

Default: rememberScrollState()

Controls scrolling inside the field. Rarely needed.

shape: ShapeOptional

Default: TextFieldDefaults.shape

Sets the outline. See the Shapes guide.

colors: TextFieldColorsOptional

Default: TextFieldDefaults.colors()

Sets colors for every state: focused, unfocused, disabled and error. Make one with TextFieldDefaults.colors or OutlinedTextFieldDefaults.colors.

contentPadding: PaddingValuesOptional

Default: TextFieldDefaults.defaultContentPadding(label, labelPosition)

Space between the edge of the field and the text.

interactionSource: MutableInteractionSource?Optional

Default: null

Lets you observe or control press, hover and focus states. Leave it null unless your code must react to them. See InteractionSource.

String

The older form. You keep the text in a String and update it in onValueChange.

@Composable
fun TextField(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    textStyle: TextStyle = LocalTextStyle.current,
    label: @Composable (() -> Unit)? = null,
    placeholder: @Composable (() -> Unit)? = null,
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    prefix: @Composable (() -> Unit)? = null,
    suffix: @Composable (() -> Unit)? = null,
    supportingText: @Composable (() -> Unit)? = null,
    isError: Boolean = false,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    singleLine: Boolean = false,
    maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
    minLines: Int = 1,
    interactionSource: MutableInteractionSource? = null,
    shape: Shape = TextFieldDefaults.shape,
    colors: TextFieldColors = TextFieldDefaults.colors(),
)

value: StringRequired

The current text. Pass your state here.

onValueChange: (String) -> UnitRequired

Called with the new text on every change. Update your state here. If you do not, the field does not change.

The String form, here as a password field
The String form, here as a password field
var password by remember { mutableStateOf("hunter2") }
TextField(
    value = password,
    onValueChange = { password = it },
    label = { Text("Password") },
    visualTransformation = PasswordVisualTransformation(),
    singleLine = true,
)

label: @Composable (() -> Unit)?Optional

Default: null

A short name for the field, like "Email". It sits inside the field and moves up when the field has text or focus.

visualTransformation: VisualTransformationOptional

Default: VisualTransformation.None

Changes how the text is shown. Use PasswordVisualTransformation() to hide a password. The String-form version of outputTransformation.

keyboardActions: KeyboardActionsOptional

Default: KeyboardActions.Default

Called when the user presses the keyboard action key. The String-form version of onKeyboardAction.

singleLine: BooleanOptional

Default: false

Set to true to keep the text on one line. The String-form version of lineLimits.

maxLines: IntOptional

Default: if (singleLine) 1 else Int.MAX_VALUE

The most lines shown before the field scrolls.

minLines: IntOptional

Default: 1

The fewest lines shown, even when empty.

Same as above: modifier, enabled, readOnly, textStyle, placeholder, leadingIcon, trailingIcon, prefix, suffix, supportingText, isError, keyboardOptions, interactionSource, shape, colors.

TextFieldValue

Like the String form, but also gives you the cursor and selection.

@Composable
fun TextField(
    value: TextFieldValue,
    onValueChange: (TextFieldValue) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    textStyle: TextStyle = LocalTextStyle.current,
    label: @Composable (() -> Unit)? = null,
    placeholder: @Composable (() -> Unit)? = null,
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    prefix: @Composable (() -> Unit)? = null,
    suffix: @Composable (() -> Unit)? = null,
    supportingText: @Composable (() -> Unit)? = null,
    isError: Boolean = false,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    singleLine: Boolean = false,
    maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
    minLines: Int = 1,
    interactionSource: MutableInteractionSource? = null,
    shape: Shape = TextFieldDefaults.shape,
    colors: TextFieldColors = TextFieldDefaults.colors(),
)

value: TextFieldValueRequired

The current text. Pass your state here.

onValueChange: (TextFieldValue) -> UnitRequired

Called with the new text on every change. Update your state here. If you do not, the field does not change.

The String form, here as a password field
The String form, here as a password field
var password by remember { mutableStateOf("hunter2") }
TextField(
    value = password,
    onValueChange = { password = it },
    label = { Text("Password") },
    visualTransformation = PasswordVisualTransformation(),
    singleLine = true,
)

Same as above: modifier, enabled, readOnly, textStyle, label, placeholder, leadingIcon, trailingIcon, prefix, suffix, supportingText, isError, visualTransformation, keyboardOptions, keyboardActions, singleLine, maxLines, minLines, interactionSource, shape, colors.

Text field types

See also