TextField
Functions summary
Unit |
@Composable |
Cmn
|
Unit |
@ComposableThis function is deprecated. This overload of TextField is deprecated in favor of the overload that uses TextFieldState to hoist its state. |
Cmn
|
Unit |
@ComposableThis function is deprecated. This overload of TextField is deprecated in favor of the overload that uses TextFieldState to hoist its state. |
Cmn
|
Functions
TextField
@Composable
fun TextField(
state: TextFieldState,
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,
isError: Boolean = false,
inputTransformation: InputTransformation? = null,
outputTransformation: OutputTransformation? = null,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
onKeyboardAction: KeyboardActionHandler? = null,
lineLimits: TextFieldLineLimits = TextFieldLineLimits.Default,
scrollState: ScrollState = rememberScrollState(),
shape: Shape = TextFieldDefaults.TextFieldShape,
colors: TextFieldColors = TextFieldDefaults.textFieldColors(),
interactionSource: MutableInteractionSource? = null
): Unit
Material Design filled text field.
Filled text fields have more visual emphasis than outlined text fields, making them stand out when surrounded by other content and components.

If you are looking for an outlined version, see OutlinedTextField.
This overload of TextField uses TextFieldState to keep track of its text content and position of the cursor or selection.
A simple single line text field looks like:
import androidx.compose.foundation.text.input.TextFieldLineLimits import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.material.Text import androidx.compose.material.TextField TextField( state = rememberTextFieldState(), label = { Text("Label") }, lineLimits = TextFieldLineLimits.SingleLine, )
You can control the initial text input and selection:
import androidx.compose.foundation.text.input.TextFieldLineLimits import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.material.Text import androidx.compose.material.TextField import androidx.compose.ui.text.TextRange val state = rememberTextFieldState("Initial text", TextRange(0, 12)) TextField(state = state, label = { Text("Label") }, lineLimits = TextFieldLineLimits.SingleLine)
You may provide a placeholder:
import androidx.compose.foundation.text.input.TextFieldLineLimits import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.material.Text import androidx.compose.material.TextField TextField( state = rememberTextFieldState(), lineLimits = TextFieldLineLimits.SingleLine, label = { Text("Email") }, placeholder = { Text("example@gmail.com") }, )
You can also provide leading and trailing icons:
import androidx.compose.foundation.text.input.TextFieldLineLimits import androidx.compose.foundation.text.input.clearText import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.material.Icon import androidx.compose.material.IconButton import androidx.compose.material.Text import androidx.compose.material.TextField import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Clear import androidx.compose.material.icons.filled.Favorite 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") } }, )
To handle the error input state, use isError parameter:
import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.input.TextFieldLineLimits import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.material.Text import androidx.compose.material.TextField import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.snapshotFlow import androidx.compose.ui.Modifier import androidx.compose.ui.semantics.error import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.input.KeyboardType val state = rememberTextFieldState() var isError by rememberSaveable { mutableStateOf(false) } fun validate(text: CharSequence) { val atIndex = text.indexOf('@') isError = atIndex < 0 || text.indexOf('.', startIndex = atIndex) < 0 } LaunchedEffect(Unit) { snapshotFlow { state.text } .collect { // Do something whenever text field value changes isError = false } } TextField( state = state, lineLimits = TextFieldLineLimits.SingleLine, label = { Text(if (isError) "Email*" else "Email") }, isError = isError, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email), onKeyboardAction = { validate(state.text) }, modifier = Modifier.semantics { // Provide localized description of the error if (isError) error("Email format is invalid.") }, )
Additionally, you may provide additional message at the bottom:
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.foundation.text.input.TextFieldLineLimits import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.material.ContentAlpha import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.material.TextField import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp 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), ) }
Hiding a software keyboard on IME action performed:
import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.material.Text import androidx.compose.material.TextField import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.text.input.ImeAction val keyboardController = LocalSoftwareKeyboardController.current TextField( state = rememberTextFieldState(), label = { Text("Label") }, keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), onKeyboardAction = { keyboardController?.hide() }, )
| Parameters | |
|---|---|
state: TextFieldState |
|
modifier: Modifier = Modifier |
a |
enabled: Boolean = true |
controls the enabled state of the |
readOnly: Boolean = false |
controls the editable state of the |
textStyle: TextStyle = LocalTextStyle.current |
the style to be applied to the input text. The default |
label: (@Composable () -> Unit)? = null |
the optional label to be displayed inside the text field container. The default text style for internal |
placeholder: (@Composable () -> Unit)? = null |
the optional placeholder to be displayed when the text field is in focus and the input text is empty. The default text style for internal |
leadingIcon: (@Composable () -> Unit)? = null |
the optional leading icon to be displayed at the beginning of the text field container |
trailingIcon: (@Composable () -> Unit)? = null |
the optional trailing icon to be displayed at the end of the text field container |
isError: Boolean = false |
indicates if the text field's current value is in error. If set to true, the label, bottom indicator and trailing icon by default will be displayed in error color |
inputTransformation: InputTransformation? = null |
Optional |
outputTransformation: OutputTransformation? = null |
An |
keyboardOptions: KeyboardOptions = KeyboardOptions.Default |
software keyboard options that contains configuration such as |
onKeyboardAction: KeyboardActionHandler? = null |
Called when the user presses the action button in the input method editor (IME), or by pressing the enter key on a hardware keyboard. By default this parameter is null, and would execute the default behavior for a received IME Action e.g., |
lineLimits: TextFieldLineLimits = TextFieldLineLimits.Default |
Whether the text field should be |
scrollState: ScrollState = rememberScrollState() |
Scroll state that manages either horizontal or vertical scroll of the text field. If |
shape: Shape = TextFieldDefaults.TextFieldShape |
the shape of the text field's container |
colors: TextFieldColors = TextFieldDefaults.textFieldColors() |
|
interactionSource: MutableInteractionSource? = null |
an optional hoisted |
TextField
@Composable
funTextField(
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,
isError: Boolean = false,
visualTransformation: VisualTransformation = VisualTransformation.None,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions(),
singleLine: Boolean = false,
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
minLines: Int = 1,
interactionSource: MutableInteractionSource? = null,
shape: Shape = TextFieldDefaults.TextFieldShape,
colors: TextFieldColors = TextFieldDefaults.textFieldColors()
): Unit
Material Design filled text field.
Filled text fields have more visual emphasis than outlined text fields, making them stand out when surrounded by other content and components.

If you are looking for an outlined version, see OutlinedTextField.
If apart from input text change you also want to observe the cursor location, selection range, or IME composition use the TextField overload with the TextFieldValue parameter instead.
| Parameters | |
|---|---|
value: String |
the input text to be shown in the text field |
onValueChange: (String) -> Unit |
the callback that is triggered when the input service updates the text. An updated text comes as a parameter of the callback |
modifier: Modifier = Modifier |
a |
enabled: Boolean = true |
controls the enabled state of the |
readOnly: Boolean = false |
controls the editable state of the |
textStyle: TextStyle = LocalTextStyle.current |
the style to be applied to the input text. The default |
label: (@Composable () -> Unit)? = null |
the optional label to be displayed inside the text field container. The default text style for internal |
placeholder: (@Composable () -> Unit)? = null |
the optional placeholder to be displayed when the text field is in focus and the input text is empty. The default text style for internal |
leadingIcon: (@Composable () -> Unit)? = null |
the optional leading icon to be displayed at the beginning of the text field container |
trailingIcon: (@Composable () -> Unit)? = null |
the optional trailing icon to be displayed at the end of the text field container |
isError: Boolean = false |
indicates if the text field's current value is in error. If set to true, the label, bottom indicator and trailing icon by default will be displayed in error color |
visualTransformation: VisualTransformation = VisualTransformation.None |
transforms the visual representation of the input |
keyboardOptions: KeyboardOptions = KeyboardOptions.Default |
software keyboard options that contains configuration such as |
keyboardActions: KeyboardActions = KeyboardActions() |
when the input service emits an IME action, the corresponding callback is called. Note that this IME action may be different from what you specified in |
singleLine: Boolean = false |
when set to true, this text field becomes a single horizontally scrolling text field instead of wrapping onto multiple lines. The keyboard will be informed to not show the return key as the |
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE |
the maximum height in terms of maximum number of visible lines. It is required that 1 <= |
minLines: Int = 1 |
the minimum height in terms of minimum number of visible lines. It is required that 1 <= |
interactionSource: MutableInteractionSource? = null |
an optional hoisted |
shape: Shape = TextFieldDefaults.TextFieldShape |
the shape of the text field's container |
colors: TextFieldColors = TextFieldDefaults.textFieldColors() |
|
TextField
@Composable
funTextField(
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,
isError: Boolean = false,
visualTransformation: VisualTransformation = VisualTransformation.None,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions(),
singleLine: Boolean = false,
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
minLines: Int = 1,
interactionSource: MutableInteractionSource? = null,
shape: Shape = TextFieldDefaults.TextFieldShape,
colors: TextFieldColors = TextFieldDefaults.textFieldColors()
): Unit
Material Design filled text field.
Filled text fields have more visual emphasis than outlined text fields, making them stand out when surrounded by other content and components.

If you are looking for an outlined version, see OutlinedTextField. For a text field specifically designed for passwords or other secure content, see SecureTextField.
This overload provides access to the input text, cursor position, selection range and IME composition. If you only want to observe an input text change, use the TextField overload with the String parameter instead.
| Parameters | |
|---|---|
value: TextFieldValue |
the input |
onValueChange: (TextFieldValue) -> Unit |
the callback that is triggered when the input service updates values in |
modifier: Modifier = Modifier |
a |
enabled: Boolean = true |
controls the enabled state of the |
readOnly: Boolean = false |
controls the editable state of the |
textStyle: TextStyle = LocalTextStyle.current |
the style to be applied to the input text. The default |
label: (@Composable () -> Unit)? = null |
the optional label to be displayed inside the text field container. The default text style for internal |
placeholder: (@Composable () -> Unit)? = null |
the optional placeholder to be displayed when the text field is in focus and the input text is empty. The default text style for internal |
leadingIcon: (@Composable () -> Unit)? = null |
the optional leading icon to be displayed at the beginning of the text field container |
trailingIcon: (@Composable () -> Unit)? = null |
the optional trailing icon to be displayed at the end of the text field container |
isError: Boolean = false |
indicates if the text field's current value is in error state. If set to true, the label, bottom indicator and trailing icon by default will be displayed in error color |
visualTransformation: VisualTransformation = VisualTransformation.None |
transforms the visual representation of the input |
keyboardOptions: KeyboardOptions = KeyboardOptions.Default |
software keyboard options that contains configuration such as |
keyboardActions: KeyboardActions = KeyboardActions() |
when the input service emits an IME action, the corresponding callback is called. Note that this IME action may be different from what you specified in |
singleLine: Boolean = false |
when set to true, this text field becomes a single horizontally scrolling text field instead of wrapping onto multiple lines. The keyboard will be informed to not show the return key as the |
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE |
the maximum height in terms of maximum number of visible lines. It is required that 1 <= |
minLines: Int = 1 |
the minimum height in terms of minimum number of visible lines. It is required that 1 <= |
interactionSource: MutableInteractionSource? = null |
an optional hoisted |
shape: Shape = TextFieldDefaults.TextFieldShape |
the shape of the text field's container |
colors: TextFieldColors = TextFieldDefaults.textFieldColors() |
|