Skip to content

Commit bcb154e

Browse files
committed
Add state based Autofill snippets
1 parent cd2e0c1 commit bcb154e

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.text
18+
19+
import androidx.compose.foundation.layout.Column
20+
import androidx.compose.foundation.layout.Spacer
21+
import androidx.compose.foundation.layout.height
22+
import androidx.compose.foundation.text.LocalAutofillHighlightColor
23+
import androidx.compose.foundation.text.input.rememberTextFieldState
24+
import androidx.compose.material.TextField
25+
import androidx.compose.material3.Text
26+
import androidx.compose.runtime.Composable
27+
import androidx.compose.runtime.CompositionLocalProvider
28+
import androidx.compose.runtime.mutableStateOf
29+
import androidx.compose.runtime.remember
30+
import androidx.compose.ui.Modifier
31+
import androidx.compose.ui.autofill.ContentType
32+
import androidx.compose.ui.graphics.Color
33+
import androidx.compose.ui.platform.LocalAutofillManager
34+
import androidx.compose.ui.semantics.contentType
35+
import androidx.compose.ui.semantics.semantics
36+
import androidx.compose.ui.text.input.TextFieldValue
37+
import androidx.compose.ui.unit.dp
38+
import com.example.compose.snippets.touchinput.Button
39+
40+
@Composable
41+
fun AddAutofill() {
42+
// [START android_compose_autofill_1]
43+
TextField(
44+
state = rememberTextFieldState(),
45+
modifier = Modifier.semantics { contentType = ContentType.Username }
46+
)
47+
// [END android_compose_autofill_1]
48+
}
49+
50+
@Composable
51+
fun AddMultipleTypesOfAutofill() {
52+
// [START android_compose_autofill_2]
53+
TextField(
54+
state = rememberTextFieldState(),
55+
modifier = Modifier.semantics {
56+
contentType = ContentType.Username + ContentType.EmailAddress
57+
}
58+
)
59+
// [END android_compose_autofill_2]
60+
}
61+
62+
@Composable
63+
fun AutofillManager() {
64+
// [START android_compose_autofill_3]
65+
val autofillManager = LocalAutofillManager.current
66+
// [END android_compose_autofill_3]
67+
}
68+
69+
@Composable
70+
fun SaveDataWithAutofill() {
71+
var textFieldValue = remember {
72+
mutableStateOf(TextFieldValue(""))
73+
}
74+
// [START android_compose_autofill_4]
75+
val autofillManager = LocalAutofillManager.current
76+
77+
Column {
78+
TextField(
79+
state = rememberTextFieldState(),
80+
modifier = Modifier.semantics { contentType = ContentType.NewUsername }
81+
)
82+
83+
Spacer(modifier = Modifier.height(16.dp))
84+
85+
TextField(
86+
state = rememberTextFieldState(),
87+
modifier = Modifier.semantics { contentType = ContentType.NewPassword }
88+
)
89+
}
90+
// [END android_compose_autofill_4]
91+
}
92+
93+
@Composable
94+
fun SaveDataWithAutofillOnClick() {
95+
// [START android_compose_autofill_5]
96+
val autofillManager = LocalAutofillManager.current
97+
98+
Column {
99+
TextField(
100+
state = rememberTextFieldState(),
101+
modifier = Modifier.semantics { contentType = ContentType.NewUsername },
102+
)
103+
104+
Spacer(modifier = Modifier.height(16.dp))
105+
106+
TextField(
107+
state = rememberTextFieldState(),
108+
modifier = Modifier.semantics { contentType = ContentType.NewPassword },
109+
)
110+
111+
// Submit button
112+
Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") }
113+
}
114+
// [END android_compose_autofill_5]
115+
}
116+
117+
@Composable
118+
fun CustomAutofillHighlight(customHighlightColor: Color = Color.Red) {
119+
// [START android_compose_autofill_6]
120+
val customHighlightColor = Color.Red
121+
122+
CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) {
123+
TextField(
124+
state = rememberTextFieldState(),
125+
modifier = Modifier.semantics { contentType = ContentType.Username }
126+
)
127+
}
128+
// [END android_compose_autofill_6]
129+
}

0 commit comments

Comments
 (0)