onFillData

Function

Common
fun SemanticsPropertyReceiver.onFillData(
    label: String? = null,
    action: ((FillableData) -> Boolean)?,
)

Action that an autofill service can invoke to fill the component with data.

The action will be called by the system, passing the FillableData that should be used to update the component's state.

This is the counterpart to the fillableData property, which is used to provide the component's current data to the autofill service.

Parameters

labelOptional label for this action.
actionAction to be performed when SemanticsActions.OnFillData is called. The lambda receives the FillableData from the autofill service.

Code Examples

AutofillableTextFieldWithFillableDataSemantics

@Composable
fun AutofillableTextFieldWithFillableDataSemantics() {
    val state = rememberTextFieldState()
    TextField(
        state = state,
        label = { Text("Enter your username here.") },
        modifier =
            Modifier.semantics {
                contentType = ContentType.Username
                // Set the fillable data with semantics.
                FillableData.createFrom(state.text)?.let { fillableData = it }
                // Replace the state value with data from the autofill provider.
                onFillData { savedAutofillValue ->
                    savedAutofillValue.textValue?.let { state.edit { replace(0, length, it) } }
                    true
                }
            },
    )
}