Skip to content

Commit 774667f

Browse files
committed
Updated to use BasicSecureTextField.
1 parent 1cefa75 commit 774667f

File tree

1 file changed

+29
-17
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/text

1 file changed

+29
-17
lines changed

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ import androidx.compose.foundation.layout.Arrangement
2929
import androidx.compose.foundation.layout.Box
3030
import androidx.compose.foundation.layout.Column
3131
import androidx.compose.foundation.layout.Row
32+
import androidx.compose.foundation.layout.Spacer
3233
import androidx.compose.foundation.layout.fillMaxWidth
3334
import androidx.compose.foundation.layout.padding
35+
import androidx.compose.foundation.layout.requiredSize
3436
import androidx.compose.foundation.layout.width
3537
import androidx.compose.foundation.shape.RoundedCornerShape
3638
import androidx.compose.foundation.text.BasicSecureTextField
@@ -648,9 +650,9 @@ private fun TextSample(samples: Map<String, @Composable () -> Unit>) {
648650

649651
private const val SAMPLE_LONG_TEXT =
650652
"Jetpack Compose is Android’s modern toolkit for building native UI. " +
651-
"It simplifies and accelerates UI development on Android bringing your apps " +
652-
"to life with less code, powerful tools, and intuitive Kotlin APIs. " +
653-
"It makes building Android UI faster and easier."
653+
"It simplifies and accelerates UI development on Android bringing your apps " +
654+
"to life with less code, powerful tools, and intuitive Kotlin APIs. " +
655+
"It makes building Android UI faster and easier."
654656

655657
@Composable
656658
@Preview
@@ -819,7 +821,7 @@ fun PhoneNumber() {
819821
// [END android_compose_text_auto_format_phone_number_textfieldconfig]
820822

821823
// [START android_compose_text_auto_format_phone_number_transformtext]
822-
class NanpVisualTransformation() : VisualTransformation {
824+
class NanpVisualTransformation : VisualTransformation {
823825

824826
override fun filter(text: AnnotatedString): TransformedText {
825827
val trimmed = if (text.text.length >= 10) text.text.substring(0..9) else text.text
@@ -880,27 +882,37 @@ fun PasswordTextField() {
880882
} else {
881883
TextObfuscationMode.RevealLastTyped
882884
},
883-
modifier = Modifier.fillMaxWidth()
885+
modifier = Modifier
886+
.fillMaxWidth()
884887
.padding(6.dp)
885888
.border(1.dp, Color.LightGray, RoundedCornerShape(6.dp))
886889
.padding(6.dp),
887-
decorator = {
888-
Icon(
889-
if (showPassword) {
890-
Icons.Filled.Visibility
891-
} else {
892-
Icons.Filled.VisibilityOff
893-
},
894-
contentDescription = "Toggle password visibility",
895-
modifier = Modifier.clickable { showPassword = !showPassword }
896-
)
890+
decorator = { innerTextField ->
891+
Box(modifier = Modifier.fillMaxWidth()) {
892+
Box(modifier = Modifier
893+
.align(Alignment.CenterStart)
894+
.padding(start = 16.dp,end = 48.dp)) {
895+
innerTextField()
896+
}
897+
Icon(
898+
if (showPassword) {
899+
Icons.Filled.Visibility
900+
} else {
901+
Icons.Filled.VisibilityOff
902+
},
903+
contentDescription = "Toggle password visibility",
904+
modifier = Modifier
905+
.align(Alignment.CenterEnd)
906+
.requiredSize(48.dp).padding(16.dp)
907+
.clickable { showPassword = !showPassword }
908+
)
909+
}
897910
}
898911
)
899912
}
900913
// [END android_compose_text_auto_format_phone_number_showhidepassword]
901914

902915

903-
904916
// [START android_compose_text_auto_format_phone_number_validatetext]
905917
class EmailViewModel : ViewModel() {
906918
var email by mutableStateOf("")
@@ -945,7 +957,7 @@ fun ValidatingInputTextField(
945957
@Preview
946958
@Composable
947959
fun ValidateInput() {
948-
val emailViewModel : EmailViewModel = viewModel<EmailViewModel>()
960+
val emailViewModel: EmailViewModel = viewModel<EmailViewModel>()
949961
ValidatingInputTextField(
950962
email = emailViewModel.email,
951963
updateState = { input -> emailViewModel.updateEmail(input) },

0 commit comments

Comments
 (0)