Skip to content

Commit

Permalink
Adding Autofill snippets
Browse files Browse the repository at this point in the history
Adding Autofill snippets
  • Loading branch information
MagicalMeghan committed Jan 28, 2025
1 parent 36d1904 commit 8fab99e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.example.compose.snippets.text

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.LocalAutofillHighlightColor
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
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.unit.dp
import com.example.compose.snippets.touchinput.Button

@Composable
fun AddAutofill() {
// [START android_compose_autofill_1]
BasicTextField(
state = remember { TextFieldState("Enter your username.") },
modifier = Modifier.semantics { contentType = ContentType.Username }
)
// [END android_compose_autofill_1]
}

@Composable
fun AddMultipleTypesOfAutofill() {
// [START android_compose_autofill_2]
Column {
BasicTextField(
state = remember { TextFieldState() },
modifier =
Modifier.semantics {
contentType = ContentType.Username + ContentType.EmailAddress
},
)
}
// [END android_compose_autofill_2]
}

@Composable
fun SaveDataWithAutofill() {
// [START android_compose_autofill_3]
// [START android_compose_autofill_4]
val autofillManager = LocalAutofillManager.current
// [END android_compose_autofill_3]

Column {
BasicTextField(
state = remember { TextFieldState() },
modifier =
Modifier.semantics {
contentType = ContentType.NewUsername
},
)

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

BasicTextField(
state = remember { TextFieldState() },
modifier =
Modifier.semantics {
contentType = ContentType.NewPassword
},
)
}
// [END android_compose_autofill_4]
}

@Composable
fun SaveDataWithAutofillOnClick() {
// [START android_compose_autofill_5]
val autofillManager = LocalAutofillManager.current
Column {
BasicTextField(
state = remember { TextFieldState() },
modifier =
Modifier.semantics {
contentType = ContentType.NewUsername
},
)

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

BasicTextField(
state = remember { TextFieldState() },
modifier =
Modifier.semantics {
contentType = ContentType.NewPassword
},
)

// Submit button
Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") }
}
// [END android_compose_autofill_5]
}

// [START android_compose_autofill_6]
@Composable
fun customizeAutofillHighlight() {
val customHighlightColor = Color.Red
val usernameState = remember { TextFieldState() }

CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) {
Column {
BasicTextField(
state = usernameState,
modifier = Modifier.semantics {
contentType = ContentType.Username
}
)
}
}
}
// [END android_compose_autofill_6]
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ androidxHiltNavigationCompose = "1.2.0"
coil = "2.7.0"
# @keep
compileSdk = "35"
compose-latest = "1.7.6"
compose-latest = "1.8.0-alpha08"
composeUiTooling = "1.4.0"
coreSplashscreen = "1.0.1"
coroutines = "1.10.1"
Expand Down

0 comments on commit 8fab99e

Please sign in to comment.