Jetpack Compose - Text fields

Text fields let users enter and edit the text using jet compose.

  • Simple Text Field Sample,
  • Simple Outlined Text Field Sample,
  • Simple Text Field with Icon,
  • Text field with Placeholder,
  • Text field with Error State,
  • Text field with Helper Message,
  • Password Text Field,
  • Text Field Sample,
  • Outlined Text Field Sample,
  • Text Field With hiding Keyboard On Ime Action,
  • Text Area.


  • Simple Text Field Sample

@Composable
fun SimpleTextFieldSample() {
var text by rememberSaveable { mutableStateOf("") }

TextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") },
singleLine = true
)
}

  • Simple Outlined Text Field Sample

@Composable
fun SimpleOutlinedTextFieldSample() {
var text by rememberSaveable { mutableStateOf("") }

OutlinedTextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") }
)
}

  • Simple Text Field with Icon
@Composable
fun TextFieldWithIcons() {
var text by rememberSaveable { mutableStateOf("") }

TextField(
value = text,
onValueChange = { text = it },
placeholder = { Text("placeholder") },
leadingIcon = { Icon(Icons.Filled.Favorite, contentDescription = "Localized description") },
trailingIcon = { Icon(Icons.Filled.Info, contentDescription = "Localized description") }
)
}
  • Text field with Placeholder

@Composable
fun TextFieldWithPlaceholder() {
var text by rememberSaveable { mutableStateOf("") }

TextField(
value = text,
onValueChange = { text = it },
label = { Text("Email") },
placeholder = { Text("example@gmail.com") }
)
}

  • Text field with Error State
@Composable
fun TextFieldWithErrorState() {
var text by rememberSaveable { mutableStateOf("") }
var isError by rememberSaveable { mutableStateOf(false) }

fun validate(text: String) {
isError = text.count() < 5
}

TextField(
value = text,
onValueChange = {
text = it
isError = false
},
singleLine = true,
label = { Text(if (isError) "Email*" else "Email") },
isError = isError,
keyboardActions = KeyboardActions { validate(text) },
modifier = Modifier.semantics {
// Provide localized description of the error
if (isError) error("Email format is invalid.")
}
)
}
  • Text field with Helper Message
@Composable
fun TextFieldWithHelperMessage() {
var text by rememberSaveable { mutableStateOf("") }

Column {
TextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") }
)
Text(
text = "Helper message",
color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.medium),
style = MaterialTheme.typography.caption,
modifier = Modifier.padding(start = 16.dp)
)
}
}
  • Password Text Field
@Composable
fun PasswordTextField() {
var password by rememberSaveable { mutableStateOf("") }
var passwordHidden by rememberSaveable { mutableStateOf(true) }
TextField(
value = password,
onValueChange = { password = it },
label = { Text("Enter password") },
visualTransformation =
if (passwordHidden) PasswordVisualTransformation() else VisualTransformation.None,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
trailingIcon = {
IconButton(onClick = { passwordHidden = !passwordHidden }) {
val visibilityIcon =
if (passwordHidden) Visibility else VisibilityOff
// Please provide localized description for accessibility services
val description = if (passwordHidden) "Show password" else "Hide password"
Icon(imageVector = visibilityIcon, contentDescription = description)
}
}
)
}
  • Text Field Sample

@Composable
fun TextFieldSample() {
var text by rememberSaveable(stateSaver = TextFieldValue.Saver) {
mutableStateOf(TextFieldValue("example", TextRange(0, 7)))
}

TextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") }
)
}

  • Outlined Text Field Sample

@Composable
fun OutlinedTextFieldSample() {
var text by rememberSaveable(stateSaver = TextFieldValue.Saver) {
mutableStateOf(TextFieldValue("example", TextRange(0, 7)))
}

OutlinedTextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") }
)
}

  • Text Field With Hide Keyboard On Ime Action

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun TextFieldWithHideKeyboardOnImeAction() {
val keyboardController = LocalSoftwareKeyboardController.current
var text by rememberSaveable { mutableStateOf("") }
TextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {
keyboardController?.hide()
// do something here
}
)
)
}


  • Text Area
@Composable
fun TextArea() {
var text by rememberSaveable { mutableStateOf("This is a very long input that extends beyond " +
"the height of the text area.") }
TextField(
value = text,
onValueChange = { text = it },
modifier = Modifier.height(100.dp),
label = { Text("Label") }
)
}


..



Comments