Skip to content

Commit

Permalink
Code snippet for Compose doc at https://developer.android.com/quick-g…
Browse files Browse the repository at this point in the history
…uides/content/show-hide-password?hl=en (show/hide password based on user toggle).

Snippet builds as-is from DAC page.
  • Loading branch information
thedmail committed Sep 16, 2024
1 parent 351fcb4 commit 9b8a865
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.basicMarquee
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -32,6 +33,10 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
Expand Down Expand Up @@ -839,3 +844,35 @@ private val firaSansFamily = FontFamily()

val LightBlue = Color(0xFF0066FF)
val Purple = Color(0xFF800080)

// [START android_compose_text_auto_format_phone_number_showhidepassword]
@Composable
fun PasswordTextField() {
var password by rememberSaveable { mutableStateOf("") }
var showPassword by remember { mutableStateOf(false) }
val passwordVisualTransformation = remember { PasswordVisualTransformation() }

TextField(
value = password,
onValueChange = { password = it },
label = { Text("Enter password") },
visualTransformation = if (showPassword) {
VisualTransformation.None
} else {
passwordVisualTransformation
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
modifier = Modifier.fillMaxWidth(),
trailingIcon = {
Icon(
if (showPassword) {
Icons.Filled.Visibility
} else {
Icons.Filled.VisibilityOff
},
contentDescription = "Toggle password visibility",
modifier = Modifier.clickable { showPassword = !showPassword })
}
)
}
// [END android_compose_text_auto_format_phone_number_showhidepassword]

0 comments on commit 9b8a865

Please sign in to comment.