androidx.compose.foundation.text
BasicTextField
Composable function · from org.jetbrains.compose.foundation:foundation:1.12.2
BasicTextField is a plain area where the user types. It has no label, no outline, no background: only the text and the cursor.
Use the Material TextField or OutlinedTextField in Material apps. Use BasicTextField for your own design, like a search field in a custom bar.
val state = rememberTextFieldState()
BasicTextField(
state = state,
textStyle = MaterialTheme.typography.bodyLarge,
decorator = { innerTextField ->
Box(Modifier.border(1.dp, Color.Gray).padding(8.dp)) { innerTextField() }
},
)
Read the text with state.text. Change it with state.setTextAndPlaceCursorAtEnd("...") or state.edit { }.
The older forms take a value and an onValueChange, like a checkbox. They still work, but new code should use the form with a state.
With a state
The current API. The text lives in a TextFieldState.
@Composable
fun BasicTextField(
state: TextFieldState,
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
inputTransformation: InputTransformation? = null,
textStyle: TextStyle = TextStyle.Default,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
onKeyboardAction: KeyboardActionHandler? = null,
lineLimits: TextFieldLineLimits = TextFieldLineLimits.Default,
onTextLayout: (Density.(getResult: () -> TextLayoutResult?) -> Unit)? = null,
interactionSource: MutableInteractionSource? = null,
cursorBrush: Brush = BasicTextFieldDefaults.CursorBrush,
outputTransformation: OutputTransformation? = null,
decorator: TextFieldDecorator? = null,
scrollState: ScrollState = rememberScrollState(),
)
state: TextFieldStateRequired
state: TextFieldStateRequiredHolds the text, the cursor and the selection. Create it with rememberTextFieldState().
modifier: ModifierOptional
modifier: ModifierOptionalDefault: Modifier
Changes size, padding, or position. See the Modifier guide.
enabled: BooleanOptional
enabled: BooleanOptionalDefault: true
Set to false to stop editing and selecting.
readOnly: BooleanOptional
readOnly: BooleanOptionalDefault: false
Set to true to allow selecting and copying, but not editing.
inputTransformation: InputTransformation?Optional
inputTransformation: InputTransformation?OptionalDefault: null
Changes or rejects what the user types, like only digits: InputTransformation.maxLength(4).
textStyle: TextStyleOptional
textStyle: TextStyleOptionalDefault: TextStyle.Default
Font, size and color of the text. Without it, the text is black, even in dark mode.
keyboardOptions: KeyboardOptionsOptional
keyboardOptions: KeyboardOptionsOptionalDefault: KeyboardOptions.Default
The kind of on-screen keyboard, like numbers or email, and its action key.
onKeyboardAction: KeyboardActionHandler?Optional
onKeyboardAction: KeyboardActionHandler?OptionalDefault: null
Called when the user presses the action key, like "Search" or "Done".
lineLimits: TextFieldLineLimitsOptional
lineLimits: TextFieldLineLimitsOptionalDefault: TextFieldLineLimits.Default
TextFieldLineLimits.SingleLine for one line, or MultiLine(minHeightInLines, maxHeightInLines).
onTextLayout: (Density.(getResult: () -> TextLayoutResult?) -> Unit)?Optional
onTextLayout: (Density.(getResult: () -> TextLayoutResult?) -> Unit)?OptionalDefault: null
Called with the measured text.
interactionSource: MutableInteractionSource?Optional
interactionSource: MutableInteractionSource?OptionalDefault: null
Lets you observe or control press, hover and focus states. Leave it null unless your code must react to them. See InteractionSource.
cursorBrush: BrushOptional
cursorBrush: BrushOptionalDefault: BasicTextFieldDefaults.CursorBrush
The color of the cursor. Set it for dark backgrounds: SolidColor(Color.White).
outputTransformation: OutputTransformation?Optional
outputTransformation: OutputTransformation?OptionalDefault: null
Changes how the text looks without changing it, like adding spaces in a phone number.
decorator: TextFieldDecorator?Optional
decorator: TextFieldDecorator?OptionalDefault: null
Draws around the text: a border, a placeholder, icons. Call innerTextField() once where the text goes.
scrollState: ScrollStateOptional
scrollState: ScrollStateOptionalDefault: rememberScrollState()
The scroll position when the text is longer than the field.
With a String
The older API. You keep the text in your own state.
@Composable
fun BasicTextField(
value: String,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
textStyle: TextStyle = TextStyle.Default,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
singleLine: Boolean = false,
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
minLines: Int = 1,
visualTransformation: VisualTransformation = VisualTransformation.None,
onTextLayout: (TextLayoutResult) -> Unit = {},
interactionSource: MutableInteractionSource? = null,
cursorBrush: Brush = SolidColor(Color.Black),
decorationBox: @Composable (innerTextField: @Composable () -> Unit) -> Unit = @Composable { innerTextField -> innerTextField() },
)
value: StringRequired
value: StringRequiredThe text to show. Update it in onValueChange.
onValueChange: (String) -> UnitRequired
onValueChange: (String) -> UnitRequiredCalled with the new text when the user types. Save it in your state.
keyboardActions: KeyboardActionsOptional
keyboardActions: KeyboardActionsOptionalDefault: KeyboardActions.Default
What the keyboard action keys do. For the older forms.
singleLine: BooleanOptional
singleLine: BooleanOptionalDefault: false
Set to true for one line. For the older forms.
maxLines: IntOptional
maxLines: IntOptionalDefault: if (singleLine) 1 else Int.MAX_VALUE
The most lines before it scrolls. For the older forms.
minLines: IntOptional
minLines: IntOptionalDefault: 1
The fewest lines of height. For the older forms.
visualTransformation: VisualTransformationOptional
visualTransformation: VisualTransformationOptionalDefault: VisualTransformation.None
Changes how the text looks, like hiding a password. For the older forms.
onTextLayout: (TextLayoutResult) -> UnitOptional
onTextLayout: (TextLayoutResult) -> UnitOptionalDefault: {}
Called with the measured text.
decorationBox: @Composable (innerTextField: @Composable () -> Unit) -> UnitOptional
decorationBox: @Composable (innerTextField: @Composable () -> Unit) -> UnitOptionalDefault: @Composable { innerTextField -> innerTextField() }
The same as decorator, for the older forms.
Same as above: modifier, enabled, readOnly, textStyle, keyboardOptions, interactionSource, cursorBrush.
With a TextFieldValue
The older API, with the cursor and selection.
@Composable
fun BasicTextField(
value: TextFieldValue,
onValueChange: (TextFieldValue) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
textStyle: TextStyle = TextStyle.Default,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
singleLine: Boolean = false,
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
minLines: Int = 1,
visualTransformation: VisualTransformation = VisualTransformation.None,
onTextLayout: (TextLayoutResult) -> Unit = {},
interactionSource: MutableInteractionSource? = null,
cursorBrush: Brush = SolidColor(Color.Black),
decorationBox: @Composable (innerTextField: @Composable () -> Unit) -> Unit = @Composable { innerTextField -> innerTextField() },
)
value: TextFieldValueRequired
value: TextFieldValueRequiredThe text to show. Update it in onValueChange.
onValueChange: (TextFieldValue) -> UnitRequired
onValueChange: (TextFieldValue) -> UnitRequiredCalled with the new text when the user types. Save it in your state.
Same as above: modifier, enabled, readOnly, textStyle, keyboardOptions, keyboardActions, singleLine, maxLines, minLines, visualTransformation, onTextLayout, interactionSource, cursorBrush, decorationBox.
Basic text fields
BasicTextFieldBasicSecureTextField