Reprator
08/24/2020, 2:04 AMTextField(
value = textValue,
keyboardType = KeyboardType.Phone,
imeAction = ImeAction.Done,
onValueChange = { textValue = it },
label = { Text("Enter Your Phone Number") },
placeholder = { Text(text = "904353455") },
onImeActionPerformed = { imeAction: ImeAction, softwareKeyboardController: SoftwareKeyboardController? ->
if (imeAction == ImeAction.Done) {
if(textValue.text.isEmpty()){
return@TextField
}
}
},
modifier = Modifier.fillMaxWidth()
)
I want to set error on this text field for phone number, but i don't find any errortext, please let me know how can i set the error on textfieldJoost Klitsie
08/24/2020, 6:43 AMReprator
08/24/2020, 12:03 PMJoost Klitsie
08/24/2020, 12:18 PMReprator
08/24/2020, 4:00 PMJoost Klitsie
08/25/2020, 7:47 AMdata class ViewState(
val textInput: String?,
val error: String?
)
@ExperimentalAnimationApi
@Composable
fun SomeComposable() {
val viewState = remember { mutableStateOf(ViewState("", null)) }
OutlinedTextViewWithError(
value = viewState.value.textInput ?: "",
onValueChange = { newValue ->
viewState.value = viewState.value.copy(
textInput = newValue,
error = if (newValue.contains("@")) "Don't use '@' character" else null)
},
error = viewState.value.error
)
}
@ExperimentalAnimationApi
@Composable
fun OutlinedTextViewWithError(value: String, onValueChange: (String) -> Unit, error: String?) {
val isError = error != null
Column {
OutlinedTextField(
value = value,
onValueChange = onValueChange,
isErrorValue = isError,
label = {})
AnimatedVisibility(visible = isError) {
Text(text = error ?: "", color = MaterialTheme.colors.error, style = MaterialTheme.typography.caption) // Choose the appropriate style
}
}
}