Skip to content
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

Adding Autofill snippets #444

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fun SharedTransitionScope.SnackItem(
SnackContents(
snack = snack,
modifier = Modifier.sharedElement(
state = rememberSharedContentState(key = snack.name),
sharedContentState = rememberSharedContentState(key = snack.name),
animatedVisibilityScope = this@AnimatedVisibility,
boundsTransform = boundsTransition,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private fun AnimatedVisibilitySharedElementShortenedExample() {
SnackContents(
snack = snack,
modifier = Modifier.sharedElement(
state = rememberSharedContentState(key = snack.name),
sharedContentState = rememberSharedContentState(key = snack.name),
animatedVisibilityScope = this@AnimatedVisibility
),
onClick = {
Expand Down Expand Up @@ -175,7 +175,7 @@ fun SharedTransitionScope.SnackEditDetails(
SnackContents(
snack = targetSnack,
modifier = Modifier.sharedElement(
state = rememberSharedContentState(key = targetSnack.name),
sharedContentState = rememberSharedContentState(key = targetSnack.name),
animatedVisibilityScope = this@AnimatedContent,
),
onClick = {
Expand Down
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]
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,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, should we just get rid of the usernameState val and replace inline on line 130 state = remember ( TextFieldState() } like the other samples do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -30,13 +30,14 @@ import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.Box
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.LocalRippleConfiguration
import androidx.compose.material.LocalUseFallbackRippleImplementation
import androidx.compose.material.RippleConfiguration
import androidx.compose.material.ripple
import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.material.ripple.RippleAlpha
import androidx.compose.material.ripple.RippleTheme
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LocalUseFallbackRippleImplementation
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
Expand Down Expand Up @@ -239,7 +240,7 @@ private class ScaleIndicationNode(
fun App() {
}

@OptIn(ExperimentalMaterialApi::class)
@OptIn(ExperimentalMaterialApi::class, ExperimentalMaterial3Api::class)
@Composable
private fun LocalUseFallbackRippleImplementationExample() {
// [START android_compose_userinteractions_localusefallbackrippleimplementation]
Expand All @@ -252,7 +253,7 @@ private fun LocalUseFallbackRippleImplementationExample() {
}

// [START android_compose_userinteractions_localusefallbackrippleimplementation_app_theme]
@OptIn(ExperimentalMaterialApi::class)
@OptIn(ExperimentalMaterialApi::class, ExperimentalMaterial3Api::class)
@Composable
fun MyAppTheme(content: @Composable () -> Unit) {
CompositionLocalProvider(LocalUseFallbackRippleImplementation provides true) {
Expand Down
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
Loading