|  | 
|  | 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 | +//noinspection UsingMaterialAndMaterial3Libraries | 
|  | 25 | +import androidx.compose.material.TextField | 
|  | 26 | +import androidx.compose.material3.Text | 
|  | 27 | +import androidx.compose.runtime.Composable | 
|  | 28 | +import androidx.compose.runtime.CompositionLocalProvider | 
|  | 29 | +import androidx.compose.runtime.mutableStateOf | 
|  | 30 | +import androidx.compose.runtime.remember | 
|  | 31 | +import androidx.compose.ui.Modifier | 
|  | 32 | +import androidx.compose.ui.autofill.ContentType | 
|  | 33 | +import androidx.compose.ui.graphics.Color | 
|  | 34 | +import androidx.compose.ui.platform.LocalAutofillManager | 
|  | 35 | +import androidx.compose.ui.semantics.contentType | 
|  | 36 | +import androidx.compose.ui.semantics.semantics | 
|  | 37 | +import androidx.compose.ui.text.input.TextFieldValue | 
|  | 38 | +import androidx.compose.ui.unit.dp | 
|  | 39 | +import com.example.compose.snippets.touchinput.Button | 
|  | 40 | + | 
|  | 41 | +@Composable | 
|  | 42 | +fun AddAutofill() { | 
|  | 43 | +    // [START android_compose_autofill_1] | 
|  | 44 | +    TextField( | 
|  | 45 | +        state = rememberTextFieldState(), | 
|  | 46 | +        modifier = Modifier.semantics { contentType = ContentType.Username } | 
|  | 47 | +    ) | 
|  | 48 | +    // [END android_compose_autofill_1] | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +@Composable | 
|  | 52 | +fun AddMultipleTypesOfAutofill() { | 
|  | 53 | +    // [START android_compose_autofill_2] | 
|  | 54 | +    TextField( | 
|  | 55 | +        state = rememberTextFieldState(), | 
|  | 56 | +        modifier = Modifier.semantics { | 
|  | 57 | +            contentType = ContentType.Username + ContentType.EmailAddress | 
|  | 58 | +        } | 
|  | 59 | +    ) | 
|  | 60 | +    // [END android_compose_autofill_2] | 
|  | 61 | +} | 
|  | 62 | + | 
|  | 63 | +@Composable | 
|  | 64 | +fun AutofillManager() { | 
|  | 65 | +    // [START android_compose_autofill_3] | 
|  | 66 | +    val autofillManager = LocalAutofillManager.current | 
|  | 67 | +    // [END android_compose_autofill_3] | 
|  | 68 | +} | 
|  | 69 | + | 
|  | 70 | +@Composable | 
|  | 71 | +fun SaveDataWithAutofill() { | 
|  | 72 | +    var textFieldValue = remember { | 
|  | 73 | +        mutableStateOf(TextFieldValue("")) | 
|  | 74 | +    } | 
|  | 75 | +    // [START android_compose_autofill_4] | 
|  | 76 | +    val autofillManager = LocalAutofillManager.current | 
|  | 77 | + | 
|  | 78 | +    Column { | 
|  | 79 | +        TextField( | 
|  | 80 | +            state = rememberTextFieldState(), | 
|  | 81 | +            modifier = Modifier.semantics { contentType = ContentType.NewUsername } | 
|  | 82 | +        ) | 
|  | 83 | + | 
|  | 84 | +        Spacer(modifier = Modifier.height(16.dp)) | 
|  | 85 | + | 
|  | 86 | +        TextField( | 
|  | 87 | +            state = rememberTextFieldState(), | 
|  | 88 | +            modifier = Modifier.semantics { contentType = ContentType.NewPassword } | 
|  | 89 | +        ) | 
|  | 90 | +    } | 
|  | 91 | +    // [END android_compose_autofill_4] | 
|  | 92 | +} | 
|  | 93 | + | 
|  | 94 | +@Composable | 
|  | 95 | +fun SaveDataWithAutofillOnClick() { | 
|  | 96 | +    // [START android_compose_autofill_5] | 
|  | 97 | +    val autofillManager = LocalAutofillManager.current | 
|  | 98 | + | 
|  | 99 | +    Column { | 
|  | 100 | +        TextField( | 
|  | 101 | +            state = rememberTextFieldState(), | 
|  | 102 | +            modifier = Modifier.semantics { contentType = ContentType.NewUsername }, | 
|  | 103 | +        ) | 
|  | 104 | + | 
|  | 105 | +        Spacer(modifier = Modifier.height(16.dp)) | 
|  | 106 | + | 
|  | 107 | +        TextField( | 
|  | 108 | +            state = rememberTextFieldState(), | 
|  | 109 | +            modifier = Modifier.semantics { contentType = ContentType.NewPassword }, | 
|  | 110 | +        ) | 
|  | 111 | + | 
|  | 112 | +        // Submit button | 
|  | 113 | +        Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") } | 
|  | 114 | +    } | 
|  | 115 | +    // [END android_compose_autofill_5] | 
|  | 116 | +} | 
|  | 117 | + | 
|  | 118 | +@Composable | 
|  | 119 | +fun CustomAutofillHighlight(customHighlightColor: Color = Color.Red) { | 
|  | 120 | +    // [START android_compose_autofill_6] | 
|  | 121 | +    val customHighlightColor = Color.Red | 
|  | 122 | + | 
|  | 123 | +    CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) { | 
|  | 124 | +        TextField( | 
|  | 125 | +            state = rememberTextFieldState(), | 
|  | 126 | +            modifier = Modifier.semantics { contentType = ContentType.Username } | 
|  | 127 | +        ) | 
|  | 128 | +    } | 
|  | 129 | +    // [END android_compose_autofill_6] | 
|  | 130 | +} | 
0 commit comments