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

← Back to Foundation

BasicSecureTextField

Component
in
Foundation
. Since 1.5.0-alpha03

Overview

Code Examples

BasicSecureTextField is a new text input component that is still in heavy development. We strongly advise against using it in production as its API and implementation are currently unstable. Many essential features such as selection, cursor, gestures, etc. may not work correctly or may not even exist yet.

BasicSecureTextField is specifically designed for password entry fields and is a preconfigured alternative to BasicTextField2. It only supports a single line of content and comes with default settings for KeyboardOptions, filter, and codepointTransformation that are appropriate for entering secure content. Additionally, some context menu actions like cut, copy, and drag are disabled for added security.

Whenever the user edits the text, onValueChange is called with the most up to date state represented by String with which developer is expected to update their state.

While focused and being edited, the caller temporarily loses direct control of the contents of the field through the value parameter. If an unexpected value is passed in during this time, the contents of the field will not be updated to reflect the value until editing is done. When editing is done (i.e. focus is lost), the field will be updated to the last value received. Use a filter to accept or reject changes during editing. For more direct control of the field contents use the BasicTextField2 overload that accepts a TextFieldState.

@param value The input String text to be shown in the text field. @param onValueChange The callback that is triggered when the user or the system updates the text. The updated text is passed as a parameter of the callback. The value passed to the callback will already have had the filter applied. @param modifier optional Modifier for this text field. @param enabled controls the enabled state of the BasicTextField2. When false, the text field will be neither editable nor focusable, the input of the text field will not be selectable. @param onSubmit Called when the user submits a form either by pressing the action button in the input method editor (IME), or by pressing the enter key on a hardware keyboard. If the user submits the form by pressing the action button in the IME, the provided IME action is passed to the function. If the user submits the form by pressing the enter key on a hardware keyboard, the defined imeAction parameter is passed to the function. Return true to indicate that the action has been handled completely, which will skip the default behavior, such as hiding the keyboard for the ImeAction.Done action. @param imeAction The IME action. This IME action is honored by keyboard and may show specific icons on the keyboard. @param textObfuscationMode Determines the method used to obscure the input text. @param keyboardType The keyboard type to be used in this text field. It is set to KeyboardType.Password by default. Use KeyboardType.NumberPassword for numerical password fields. @param inputTransformation Optional InputTransformation that will be used to filter changes to the TextFieldState made by the user. The filter will be applied to changes made by hardware and software keyboard events, pasting or dropping text, accessibility services, and tests. The filter will not be applied when changing the value programmatically, or when the filter is changed. If the filter is changed on an existing text field, it will be applied to the next user edit. the filter will not immediately affect the current value. @param textStyle Style configuration for text content that's displayed in the editor. @param interactionSource the MutableInteractionSource representing the stream of Interactions for this TextField. You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this TextField for different Interactions. @param cursorBrush Brush to paint cursor with. If SolidColor with Color.Unspecified provided, there will be no cursor drawn @param onTextLayout Callback that is executed when a new text layout is calculated. A TextLayoutResult object that callback provides contains paragraph information, size of the text, baselines and other details. The callback can be used to add additional decoration or functionality to the text. For example, to draw a cursor or selection around the text. Density scope is the one that was used while creating the given text layout. @param decorator Allows to add decorations around text field, such as icon, placeholder, helper messages or similar, and automatically increase the hit target area of the text field. @param scrollState Used to manage the horizontal scroll when the input content exceeds the bounds of the text field. It controls the state of the scroll for the text field.

Overloads

BasicSecureTextField

@ExperimentalFoundationApi
@Composable
fun BasicSecureTextField(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    onSubmit: ImeActionHandler? = null,
    imeAction: ImeAction = ImeAction.Default,
    textObfuscationMode: TextObfuscationMode = TextObfuscationMode.RevealLastTyped,
    keyboardType: KeyboardType = KeyboardType.Password,
    enabled: Boolean = true,
    inputTransformation: InputTransformation? = null,
    textStyle: TextStyle = TextStyle.Default,
    interactionSource: MutableInteractionSource? = null,
    cursorBrush: Brush = SolidColor(Color.Black),
    onTextLayout: Density.(getResult: () -> TextLayoutResult?) -> Unit = {},
    decorator: TextFieldDecorator? = null,
    scrollState: ScrollState = rememberScrollState(),
)

Parameters

NameDescription
valueThe input String text to be shown in the text field.
onValueChangeThe callback that is triggered when the user or the system updates the text. The updated text is passed as a parameter of the callback. The value passed to the callback will already have had the filter applied.
modifieroptional Modifier for this text field.
enabledcontrols the enabled state of the BasicTextField2. When false, the text field will be neither editable nor focusable, the input of the text field will not be selectable.
onSubmitCalled when the user submits a form either by pressing the action button in the input method editor (IME), or by pressing the enter key on a hardware keyboard. If the user submits the form by pressing the action button in the IME, the provided IME action is passed to the function. If the user submits the form by pressing the enter key on a hardware keyboard, the defined imeAction parameter is passed to the function. Return true to indicate that the action has been handled completely, which will skip the default behavior, such as hiding the keyboard for the ImeAction.Done action.
imeActionThe IME action. This IME action is honored by keyboard and may show specific icons on the keyboard.
textObfuscationModeDetermines the method used to obscure the input text.
keyboardTypeThe keyboard type to be used in this text field. It is set to KeyboardType.Password by default. Use KeyboardType.NumberPassword for numerical password fields.
inputTransformationOptional InputTransformation that will be used to filter changes to the TextFieldState made by the user. The filter will be applied to changes made by hardware and software keyboard events, pasting or dropping text, accessibility services, and tests. The filter will not be applied when changing the value programmatically, or when the filter is changed. If the filter is changed on an existing text field, it will be applied to the next user edit. the filter will not immediately affect the current value.
textStyleStyle configuration for text content that's displayed in the editor.
interactionSourcethe MutableInteractionSource representing the stream of Interactions for this TextField. You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this TextField for different Interactions.
cursorBrushBrush to paint cursor with. If SolidColor with Color.Unspecified provided, there will be no cursor drawn
onTextLayoutCallback that is executed when a new text layout is calculated. A TextLayoutResult object that callback provides contains paragraph information, size of the text, baselines and other details. The callback can be used to add additional decoration or functionality to the text. For example, to draw a cursor or selection around the text. Density scope is the one that was used while creating the given text layout.
decoratorAllows to add decorations around text field, such as icon, placeholder, helper messages or similar, and automatically increase the hit target area of the text field.
scrollStateUsed to manage the horizontal scroll when the input content exceeds the bounds of the text field. It controls the state of the scroll for the text field

BasicSecureTextField

@ExperimentalFoundationApi
// This takes a composable lambda, but it is not primarily a container.
@Suppress("ComposableLambdaParameterPosition")
@Composable
fun BasicSecureTextField(
    state: TextFieldState,
    modifier: Modifier = Modifier,
    onSubmit: ImeActionHandler? = null,
    imeAction: ImeAction = ImeAction.Default,
    textObfuscationMode: TextObfuscationMode = TextObfuscationMode.RevealLastTyped,
    keyboardType: KeyboardType = KeyboardType.Password,
    enabled: Boolean = true,
    inputTransformation: InputTransformation? = null,
    textStyle: TextStyle = TextStyle.Default,
    interactionSource: MutableInteractionSource? = null,
    cursorBrush: Brush = SolidColor(Color.Black),
    onTextLayout: Density.(getResult: () -> TextLayoutResult?) -> Unit = {},
    decorator: TextFieldDecorator? = null,
    scrollState: ScrollState = rememberScrollState(),
    // Last parameter must not be a function unless it's intended to be commonly used as a trailing
    // lambda.
)

Parameters

NameDescription
stateTextFieldState object that holds the internal state of a BasicTextField2.
modifieroptional Modifier for this text field.
enabledcontrols the enabled state of the BasicTextField2. When false, the text field will be neither editable nor focusable, the input of the text field will not be selectable.
onSubmitCalled when the user submits a form either by pressing the action button in the input method editor (IME), or by pressing the enter key on a hardware keyboard. If the user submits the form by pressing the action button in the IME, the provided IME action is passed to the function. If the user submits the form by pressing the enter key on a hardware keyboard, the defined imeAction parameter is passed to the function. Return true to indicate that the action has been handled completely, which will skip the default behavior, such as hiding the keyboard for the ImeAction.Done action.
imeActionThe IME action. This IME action is honored by keyboard and may show specific icons on the keyboard.
textObfuscationModeDetermines the method used to obscure the input text.
keyboardTypeThe keyboard type to be used in this text field. It is set to KeyboardType.Password by default. Use KeyboardType.NumberPassword for numerical password fields.
inputTransformationOptional InputTransformation that will be used to filter changes to the TextFieldState made by the user. The filter will be applied to changes made by hardware and software keyboard events, pasting or dropping text, accessibility services, and tests. The filter will not be applied when changing the state programmatically, or when the filter is changed. If the filter is changed on an existing text field, it will be applied to the next user edit. the filter will not immediately affect the current state.
textStyleStyle configuration for text content that's displayed in the editor.
interactionSourcethe MutableInteractionSource representing the stream of Interactions for this TextField. You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this TextField for different Interactions.
cursorBrushBrush to paint cursor with. If SolidColor with Color.Unspecified provided, there will be no cursor drawn
onTextLayoutCallback that is executed when a new text layout is calculated. A TextLayoutResult object that callback provides contains paragraph information, size of the text, baselines and other details. The callback can be used to add additional decoration or functionality to the text. For example, to draw a cursor or selection around the text. Density scope is the one that was used while creating the given text layout.
decoratorAllows to add decorations around text field, such as icon, placeholder, helper messages or similar, and automatically increase the hit target area of the text field.
scrollStateUsed to manage the horizontal scroll when the input content exceeds the bounds of the text field. It controls the state of the scroll for the text field