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

Holds the text, the cursor and the selection. Create it with rememberTextFieldState().

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. See the Modifier guide.

enabled: BooleanOptional

Default: true

Set to false to stop editing and selecting.

readOnly: BooleanOptional

Default: false

Set to true to allow selecting and copying, but not editing.

inputTransformation: InputTransformation?Optional

Default: null

Changes or rejects what the user types, like only digits: InputTransformation.maxLength(4).

textStyle: TextStyleOptional

Default: TextStyle.Default

Font, size and color of the text. Without it, the text is black, even in dark mode.

keyboardOptions: KeyboardOptionsOptional

Default: KeyboardOptions.Default

The kind of on-screen keyboard, like numbers or email, and its action key.

onKeyboardAction: KeyboardActionHandler?Optional

Default: null

Called when the user presses the action key, like "Search" or "Done".

lineLimits: TextFieldLineLimitsOptional

Default: TextFieldLineLimits.Default

TextFieldLineLimits.SingleLine for one line, or MultiLine(minHeightInLines, maxHeightInLines).

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

Default: null

Called with the measured 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.

cursorBrush: BrushOptional

Default: BasicTextFieldDefaults.CursorBrush

The color of the cursor. Set it for dark backgrounds: SolidColor(Color.White).

outputTransformation: OutputTransformation?Optional

Default: null

Changes how the text looks without changing it, like adding spaces in a phone number.

decorator: TextFieldDecorator?Optional

Default: null

Draws around the text: a border, a placeholder, icons. Call innerTextField() once where the text goes.

scrollState: ScrollStateOptional

Default: 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

The text to show. Update it in onValueChange.

onValueChange: (String) -> UnitRequired

Called with the new text when the user types. Save it in your state.

keyboardActions: KeyboardActionsOptional

Default: KeyboardActions.Default

What the keyboard action keys do. For the older forms.

singleLine: BooleanOptional

Default: false

Set to true for one line. For the older forms.

maxLines: IntOptional

Default: if (singleLine) 1 else Int.MAX_VALUE

The most lines before it scrolls. For the older forms.

minLines: IntOptional

Default: 1

The fewest lines of height. For the older forms.

visualTransformation: VisualTransformationOptional

Default: VisualTransformation.None

Changes how the text looks, like hiding a password. For the older forms.

onTextLayout: (TextLayoutResult) -> UnitOptional

Default: {}

Called with the measured text.

decorationBox: @Composable (innerTextField: @Composable () -> Unit) -> UnitOptional

Default: @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

The text to show. Update it in onValueChange.

onValueChange: (TextFieldValue) -> UnitRequired

Called 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