Input content type - Jetpack Compose
In Jetpack Compose, Autofill
can be used to automatically fill TextField
with predefined data, such as email, credit card information, etc.
You can set AutoFillType
to specify which type of data should be prompted to user. The following values are defined:
AddressAuxiliaryDetails
: auxiliary address detailsAddressCountry
: country name/ codeAddressLocality
: address locality (city/town)AddressRegion
: region/ stateAddressStreet
: street addressBirthDateDay
: birth day(of the month)BirthDateFull
: full birth dateBirthDateMonth
: birth monthBirthDateYear
: birth yearCreditCardExpirationDate
: credit card expiration dateCreditCardExpirationDay
: credit card expiration dayCreditCardExpirationMonth
: credit card expiration monthCreditCardExpirationYear
: credit card expiration yearCreditCardNumber
: credit card numberCreditCardSecurityCode
: credit card security codeEmailAddress
: email addressGender
: genderNewPassword
:newly created password for save/updateNewUsername
: newly created username for save/updatePassword
: passwordPersonFirstName
: person's first/ given namePersonFullName
: person's full namePersonLastName
: person's last/ family namePersonMiddleInitial
: person's middle initialPersonMiddleName
: person's middle namePersonNamePrefix
: person's name prefixPersonNameSuffix
: person's name suffixPhoneCountryCode
: phone number's country codePhoneNumber
: phone number with country codePhoneNumberDevice
: device's phone number usually for Sign Up / OTP flowsPhoneNumberNational
: phone number without country codePostalAddress
: postal addressPostalCode
: postal codePostalCodeExtended
: extended ZIP/ POSTAL codeSmsOtpCode
: SMS One Time Password (OTP)Username
: username
// Create custom composable with AutoFill to later wrap it with TextField
@ExperimentalComposeUiApi
@Composable
private fun Autofill(
autofillTypes: List<AutofillType>,
onFill: ((String) -> Unit),
content: @Composable BoxScope.() -> Unit
) {
val autofill = LocalAutofill.current
val autofillTree = LocalAutofillTree.current
val autofillNode =
remember(autofillTypes, onFill) {
AutofillNode(onFill = onFill, autofillTypes = autofillTypes)
}
Box(
modifier =
Modifier.onFocusChanged {
if (it.isFocused) {
autofill?.requestAutofillForNode(autofillNode)
} else {
autofill?.cancelAutofillForNode(autofillNode)
}
}
.onGloballyPositioned { autofillNode.boundingBox = it.boundsInWindow() },
content = content
)
DisposableEffect(autofillNode) {
autofillTree.children[autofillNode.id] = autofillNode
onDispose { autofillTree.children.remove(autofillNode.id) }
}
}
// AutoFill composable usage
var name by
rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue("")) }
Autofill(
// We can pass here multiple AutofillTypes
autofillTypes = listOf(AutofillType.PersonFullName, AutofillType.PersonLastName),
onFill = { name = TextFieldValue(it) }
) {
OutlinedTextField(
value = name,
onValueChange = { name = it },
label = { Text("Name") },
)
}
Important: in Jetpack Compose 1.7 Autofill
is disabled, due to massive performance impact. As Google developers states, it should be available in Compose 1.8. You can track current feature status by following google issue tracker ticket
.