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

← Back to Foundation

BaseTextField

Component
in
Foundation
. Since 0.1.0-dev16

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 TextFieldValue. 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 BaseTextField in order to have the final state of the text being displayed.

Overloads

BaseTextField

@Deprecated(
    "Use BasicTextField instead.",
    replaceWith = ReplaceWith(
        "BasicTextField(value, onValueChange, modifier, textStyle.merge(TextStyle(color = " +
            "textColor)), keyboardType, imeAction, onImeActionPerformed, visualTransformation, " +
            "onTextLayout, onTextInputStarted, cursorColor)",
        "androidx.compose.foundation.text.BasicTextField",
        "androidx.compose.foundation.AmbientContentColor",
        "androidx.compose.foundation.AmbientTextStyle",
        "androidx.compose.ui.text.TextStyle",
    )
)
@Composable
@ExperimentalFoundationApi
fun BaseTextField(
    value: TextFieldValue,
    onValueChange: (TextFieldValue) -> Unit,
    modifier: Modifier = Modifier,
    textColor: Color = Color.Unspecified,
    textStyle: TextStyle = TextStyle(),
    keyboardType: KeyboardType = KeyboardType.Text,
    imeAction: ImeAction = ImeAction.Default,
    onImeActionPerformed: (ImeAction) -> Unit = {},
    visualTransformation: VisualTransformation = VisualTransformation.None,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    onTextInputStarted: (SoftwareKeyboardController) -> Unit = {},
    cursorColor: Color = AmbientContentColor.current
)

Parameters

NameDescription
valueThe TextFieldValue to be shown in the BaseTextField.
onValueChangeCalled when the input service updates values in TextFieldValue.
modifieroptional Modifier for this text field.
textColorColor to apply to the text. If Color.Unspecified, and textStyle has no color set, this will be AmbientContentColor.
textStyleStyle configuration that applies at character level such as color, font etc. The default textStyle uses the AmbientTextStyle 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.
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. If Color.Unspecified, there will be no cursor drawn @see TextFieldValue @see ImeAction @see KeyboardType @see VisualTransformatio