Skip to content

Commit 2db5704

Browse files
riggaroothedmail
andauthored
Password update to use BasicSecureTextField (#357)
* Code snippet for Compose doc at https://developer.android.com/quick-guides/content/animate-text?hl=en (Animate text character-by-character). This commit slightly modifies (makes buildable in our repo) the existing code on the current DAC page. That code, in turn, was BNR's simplified version of Xoogler astamato's Medium article at https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5 * Code snippet for Compose doc at https://developer.android.com/quick-guides/content/animate-text?hl=en (Animate text character-by-character). This commit slightly modifies (makes buildable in our repo) the existing code on the current DAC page. That code, in turn, was BNR's simplified version of Xoogler astamato's Medium article at https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5 * Apply Spotless * Fix email input snippet * Migrate to use BasicSecureTextField. * Updated to use BasicSecureTextField. * Apply Spotless * Apply Spotless --------- Co-authored-by: dmail <[email protected]> Co-authored-by: thedmail <[email protected]> Co-authored-by: riggaroo <[email protected]>
1 parent eb6b518 commit 2db5704

File tree

1 file changed

+40
-23
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/text

1 file changed

+40
-23
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/text/TextSnippets.kt

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ import androidx.compose.foundation.layout.Column
3030
import androidx.compose.foundation.layout.Row
3131
import androidx.compose.foundation.layout.fillMaxWidth
3232
import androidx.compose.foundation.layout.padding
33+
import androidx.compose.foundation.layout.requiredSize
3334
import androidx.compose.foundation.layout.width
35+
import androidx.compose.foundation.shape.RoundedCornerShape
36+
import androidx.compose.foundation.text.BasicSecureTextField
3437
import androidx.compose.foundation.text.KeyboardOptions
38+
import androidx.compose.foundation.text.input.TextFieldState
39+
import androidx.compose.foundation.text.input.TextObfuscationMode
3540
import androidx.compose.foundation.text.selection.DisableSelection
3641
import androidx.compose.foundation.text.selection.SelectionContainer
3742
import androidx.compose.material.icons.Icons
@@ -812,7 +817,7 @@ fun PhoneNumber() {
812817
// [END android_compose_text_auto_format_phone_number_textfieldconfig]
813818

814819
// [START android_compose_text_auto_format_phone_number_transformtext]
815-
class NanpVisualTransformation() : VisualTransformation {
820+
class NanpVisualTransformation : VisualTransformation {
816821

817822
override fun filter(text: AnnotatedString): TransformedText {
818823
val trimmed = if (text.text.length >= 10) text.text.substring(0..9) else text.text
@@ -862,31 +867,43 @@ val Purple = Color(0xFF800080)
862867
// [START android_compose_text_showhidepassword]
863868
@Composable
864869
fun PasswordTextField() {
865-
var password by rememberSaveable { mutableStateOf("") }
870+
val state = remember { TextFieldState() }
866871
var showPassword by remember { mutableStateOf(false) }
867-
val passwordVisualTransformation = remember { PasswordVisualTransformation() }
868-
869-
TextField(
870-
value = password,
871-
onValueChange = { password = it },
872-
label = { Text("Enter password") },
873-
visualTransformation = if (showPassword) {
874-
VisualTransformation.None
872+
BasicSecureTextField(
873+
state = state,
874+
textObfuscationMode =
875+
if (showPassword) {
876+
TextObfuscationMode.Visible
875877
} else {
876-
passwordVisualTransformation
878+
TextObfuscationMode.RevealLastTyped
877879
},
878-
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
879-
modifier = Modifier.fillMaxWidth(),
880-
trailingIcon = {
881-
Icon(
882-
if (showPassword) {
883-
Icons.Filled.Visibility
884-
} else {
885-
Icons.Filled.VisibilityOff
886-
},
887-
contentDescription = "Toggle password visibility",
888-
modifier = Modifier.clickable { showPassword = !showPassword }
889-
)
880+
modifier = Modifier
881+
.fillMaxWidth()
882+
.padding(6.dp)
883+
.border(1.dp, Color.LightGray, RoundedCornerShape(6.dp))
884+
.padding(6.dp),
885+
decorator = { innerTextField ->
886+
Box(modifier = Modifier.fillMaxWidth()) {
887+
Box(
888+
modifier = Modifier
889+
.align(Alignment.CenterStart)
890+
.padding(start = 16.dp, end = 48.dp)
891+
) {
892+
innerTextField()
893+
}
894+
Icon(
895+
if (showPassword) {
896+
Icons.Filled.Visibility
897+
} else {
898+
Icons.Filled.VisibilityOff
899+
},
900+
contentDescription = "Toggle password visibility",
901+
modifier = Modifier
902+
.align(Alignment.CenterEnd)
903+
.requiredSize(48.dp).padding(16.dp)
904+
.clickable { showPassword = !showPassword }
905+
)
906+
}
890907
}
891908
)
892909
}

0 commit comments

Comments
 (0)