-
Notifications
You must be signed in to change notification settings - Fork 237
Adding Autofill snippets #444
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
8fab99e
b510be2
996e15e
bab62b6
3b9233a
315d4f1
4266212
b50ebf5
6722af8
8bc39e3
86ee57c
105645a
347c502
635ea2c
8927d2d
77bd621
cfeb99f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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] | ||
MagicalMeghan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val autofillManager = LocalAutofillManager.current | ||
MagicalMeghan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// [END android_compose_autofill_3] | ||
|
||
Column { | ||
BasicTextField( | ||
state = remember { TextFieldState() }, | ||
modifier = | ||
Modifier.semantics { | ||
contentType = ContentType.NewUsername | ||
}, | ||
) | ||
|
||
Spacer(modifier = Modifier.height(16.dp)) | ||
MagicalMeghan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, should we just get rid of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah i'll update |
||
modifier = Modifier.semantics { | ||
contentType = ContentType.Username | ||
} | ||
) | ||
} | ||
} | ||
} | ||
// [END android_compose_autofill_6] |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
composeUiTooling = "1.4.0" | ||||||
coreSplashscreen = "1.0.1" | ||||||
coroutines = "1.10.1" | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.