Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Autofill snippets to use state based TFs #488

Open
wants to merge 2 commits into
base: autofill-snippets
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,35 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.text.LocalAutofillHighlightColor
import androidx.compose.material3.TextField
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.autofill.ContentType
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalAutofillManager
import androidx.compose.ui.semantics.contentType
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import com.example.compose.snippets.touchinput.Button

@Composable
fun AddAutofill() {
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}
// [START android_compose_autofill_1]
TextField(
value = textFieldValue.value,
onValueChange = {textFieldValue.value = it},
state = rememberTextFieldState(),
modifier = Modifier.semantics { contentType = ContentType.Username }
)
// [END android_compose_autofill_1]
}

@Composable
fun AddMultipleTypesOfAutofill() {
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}
// [START android_compose_autofill_2]
TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
state = rememberTextFieldState(),
modifier = Modifier.semantics {
contentType = ContentType.Username + ContentType.EmailAddress
}
Expand All @@ -75,24 +65,19 @@ fun AutofillManager() {

@Composable
fun SaveDataWithAutofill() {
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}
// [START android_compose_autofill_4]
val autofillManager = LocalAutofillManager.current

Column {
TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
state = rememberTextFieldState(),
modifier = Modifier.semantics { contentType = ContentType.NewUsername }
)

Spacer(modifier = Modifier.height(16.dp))

TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
state = rememberTextFieldState(),
modifier = Modifier.semantics { contentType = ContentType.NewPassword }
)
}
Expand All @@ -101,24 +86,19 @@ fun SaveDataWithAutofill() {

@Composable
fun SaveDataWithAutofillOnClick() {
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}
// [START android_compose_autofill_5]
val autofillManager = LocalAutofillManager.current

Column {
TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
state = rememberTextFieldState(),
modifier = Modifier.semantics { contentType = ContentType.NewUsername },
)

Spacer(modifier = Modifier.height(16.dp))

TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
state = rememberTextFieldState(),
modifier = Modifier.semantics { contentType = ContentType.NewPassword },
)

Expand All @@ -130,16 +110,12 @@ fun SaveDataWithAutofillOnClick() {

@Composable
fun CustomAutofillHighlight(customHighlightColor: Color = Color.Red) {
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}
// [START android_compose_autofill_6]
val customHighlightColor = Color.Red

CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) {
TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
state = rememberTextFieldState(),
modifier = Modifier.semantics { contentType = ContentType.Username }
)
}
Expand Down
9 changes: 5 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[versions]
accompanist = "0.36.0"
androidGradlePlugin = "8.8.0"
androidx-activity-compose = "1.10.0"
androidx-activity-compose = "1.10.1"
androidx-appcompat = "1.7.0"
androidx-compose-bom = "2025.01.00"
androidx-compose-bom = "2025.03.01"
androidx-compose-ui-test = "1.7.0-alpha08"
androidx-constraintlayout = "2.2.0"
androidx-constraintlayout-compose = "1.1.0"
Expand Down Expand Up @@ -38,7 +38,8 @@ kotlin = "2.1.0"
kotlinxSerializationJson = "1.8.0"
ksp = "2.1.0-1.0.29"
maps-compose = "6.4.1"
material = "1.13.0-alpha09"
material = "1.13.0-alpha12"
material3-alpha = "1.4.0-alpha11"
material3-adaptive = "1.0.0"
material3-adaptive-navigation-suite = "1.3.1"
media3 = "1.5.1"
Expand Down Expand Up @@ -77,7 +78,7 @@ androidx-compose-foundation-layout = { module = "androidx.compose.foundation:fou
androidx-compose-material = { module = "androidx.compose.material:material", version.ref = "compose-latest" }
androidx-compose-material-iconsExtended = { module = "androidx.compose.material:material-icons-extended" }
androidx-compose-material-ripple = { module = "androidx.compose.material:material-ripple", version.ref = "compose-latest" }
androidx-compose-material3 = { module = "androidx.compose.material3:material3" }
androidx-compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "material3-alpha" }
androidx-compose-material3-adaptive = { module = "androidx.compose.material3.adaptive:adaptive", version.ref = "material3-adaptive" }
androidx-compose-material3-adaptive-layout = { module = "androidx.compose.material3.adaptive:adaptive-layout", version.ref = "material3-adaptive" }
androidx-compose-material3-adaptive-navigation = { module = "androidx.compose.material3.adaptive:adaptive-navigation", version.ref = "material3-adaptive" }
Expand Down
Loading