Skip to content

Commit 8fab99e

Browse files
committed
Adding Autofill snippets
Adding Autofill snippets
1 parent 36d1904 commit 8fab99e

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.example.compose.snippets.text
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.Spacer
5+
import androidx.compose.foundation.layout.height
6+
import androidx.compose.foundation.text.BasicTextField
7+
import androidx.compose.foundation.text.LocalAutofillHighlightColor
8+
import androidx.compose.foundation.text.input.TextFieldState
9+
import androidx.compose.material3.Text
10+
import androidx.compose.runtime.Composable
11+
import androidx.compose.runtime.CompositionLocalProvider
12+
import androidx.compose.runtime.remember
13+
import androidx.compose.ui.Modifier
14+
import androidx.compose.ui.autofill.ContentType
15+
import androidx.compose.ui.graphics.Color
16+
import androidx.compose.ui.platform.LocalAutofillManager
17+
import androidx.compose.ui.semantics.contentType
18+
import androidx.compose.ui.semantics.semantics
19+
import androidx.compose.ui.unit.dp
20+
import com.example.compose.snippets.touchinput.Button
21+
22+
@Composable
23+
fun AddAutofill() {
24+
// [START android_compose_autofill_1]
25+
BasicTextField(
26+
state = remember { TextFieldState("Enter your username.") },
27+
modifier = Modifier.semantics { contentType = ContentType.Username }
28+
)
29+
// [END android_compose_autofill_1]
30+
}
31+
32+
@Composable
33+
fun AddMultipleTypesOfAutofill() {
34+
// [START android_compose_autofill_2]
35+
Column {
36+
BasicTextField(
37+
state = remember { TextFieldState() },
38+
modifier =
39+
Modifier.semantics {
40+
contentType = ContentType.Username + ContentType.EmailAddress
41+
},
42+
)
43+
}
44+
// [END android_compose_autofill_2]
45+
}
46+
47+
@Composable
48+
fun SaveDataWithAutofill() {
49+
// [START android_compose_autofill_3]
50+
// [START android_compose_autofill_4]
51+
val autofillManager = LocalAutofillManager.current
52+
// [END android_compose_autofill_3]
53+
54+
Column {
55+
BasicTextField(
56+
state = remember { TextFieldState() },
57+
modifier =
58+
Modifier.semantics {
59+
contentType = ContentType.NewUsername
60+
},
61+
)
62+
63+
Spacer(modifier = Modifier.height(16.dp))
64+
65+
BasicTextField(
66+
state = remember { TextFieldState() },
67+
modifier =
68+
Modifier.semantics {
69+
contentType = ContentType.NewPassword
70+
},
71+
)
72+
}
73+
// [END android_compose_autofill_4]
74+
}
75+
76+
@Composable
77+
fun SaveDataWithAutofillOnClick() {
78+
// [START android_compose_autofill_5]
79+
val autofillManager = LocalAutofillManager.current
80+
Column {
81+
BasicTextField(
82+
state = remember { TextFieldState() },
83+
modifier =
84+
Modifier.semantics {
85+
contentType = ContentType.NewUsername
86+
},
87+
)
88+
89+
Spacer(modifier = Modifier.height(16.dp))
90+
91+
BasicTextField(
92+
state = remember { TextFieldState() },
93+
modifier =
94+
Modifier.semantics {
95+
contentType = ContentType.NewPassword
96+
},
97+
)
98+
99+
// Submit button
100+
Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") }
101+
}
102+
// [END android_compose_autofill_5]
103+
}
104+
105+
// [START android_compose_autofill_6]
106+
@Composable
107+
fun customizeAutofillHighlight() {
108+
val customHighlightColor = Color.Red
109+
val usernameState = remember { TextFieldState() }
110+
111+
CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) {
112+
Column {
113+
BasicTextField(
114+
state = usernameState,
115+
modifier = Modifier.semantics {
116+
contentType = ContentType.Username
117+
}
118+
)
119+
}
120+
}
121+
}
122+
// [END android_compose_autofill_6]

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ androidxHiltNavigationCompose = "1.2.0"
2323
coil = "2.7.0"
2424
# @keep
2525
compileSdk = "35"
26-
compose-latest = "1.7.6"
26+
compose-latest = "1.8.0-alpha08"
2727
composeUiTooling = "1.4.0"
2828
coreSplashscreen = "1.0.1"
2929
coroutines = "1.10.1"

0 commit comments

Comments
 (0)