State of Compose 2023 results are in! Click here to learn more

← Back to Foundation

TextField

Component
in
Foundation
. Since 0.1.0-dev15

Overview

Code Examples

Composable that enables users to edit text via hardware or software keyboard.

Whenever the user edits the text, onValueChange is called with the most up to date state represented by androidx.ui.input.TextFieldValue. androidx.ui.input.TextFieldValue contains the text entered by user, as well as selection, cursor and text composition information. Please check TextFieldValue for the description of its contents.

It is crucial that the value provided in the onValueChange is fed back into TextField in order to have the final state of the text being displayed.

Overloads

TextField

@Composable
fun TextField(
    value: androidx.ui.input.TextFieldValue,
    onValueChange: (androidx.ui.input.TextFieldValue) -> Unit,
    modifier: Modifier = Modifier,
    textColor: Color = Color.Unset,
    textStyle: TextStyle = currentTextStyle(),
    keyboardType: KeyboardType = KeyboardType.Text,
    imeAction: ImeAction = ImeAction.Unspecified,
    onImeActionPerformed: (ImeAction) -> Unit = {},
    onFocusChanged: (Boolean) -> Unit = {},
    visualTransformation: VisualTransformation = VisualTransformation.None,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    onTextInputStarted: (SoftwareKeyboardController) -> Unit = {},
    cursorColor: Color = contentColor()
)

Parameters

NameDescription
valueThe androidx.ui.input.TextFieldValue to be shown in the TextField.
onValueChangeCalled when the input service updates values in androidx.ui.input.TextFieldValue.
modifieroptional Modifier for this text field.
textColorColor to apply to the text. If Color.Unset, and textStyle has no color set, this will be contentColor.
textStyleStyle configuration that applies at character level such as color, font etc. The default textStyle uses the currentTextStyle defined by the theme
keyboardTypeThe keyboard type to be used in this text field. Note that this input type is honored by IME and shows corresponding keyboard but this is not guaranteed. For example, some IME may send non-ASCII character even if you set KeyboardType.Ascii.
imeActionThe IME action. This IME action is honored by IME and may show specific icons on the keyboard. For example, search icon may be shown if ImeAction.Search is specified. Then, when user tap that key, the onImeActionPerformed callback is called with specified ImeAction.
onImeActionPerformedCalled when the input service requested an IME action. When the input service emitted an IME action, this callback is called with the emitted IME action. Note that this IME action may be different from what you specified in imeAction.
onFocusChangedCalled with true value when the input field gains focus and with false value when the input field loses focus. Use FocusModifier.requestFocus to obtain text input focus to this TextField.
visualTransformationThe visual transformation filter for changing the visual representation of the input. By default no visual transformation is applied.
onTextLayoutCallback that is executed when a new text layout is calculated.
onTextInputStartedCallback that is executed when the initialization has done for communicating with platform text input service, e.g. software keyboard on Android. Called with SoftwareKeyboardController instance which can be used for requesting input show/hide software keyboard.
cursorColorColor of the cursor. @see TextFieldValue @see ImeAction @see KeyboardType @see VisualTransformatio