Tobias Preuss
01/11/2023, 3:20 PMcom.google.android.material.textfield.TextInputLayout
incl. hint outline, character limit text, error handling? Is there any recommendable open source library out there?Tgo1014
01/11/2023, 3:22 PMOutlinedTextField
Tobias Preuss
01/11/2023, 3:35 PMOutlinedTextField
is what I started off with, too. But it does not offer the supported text below, automatic error handling, ... and maybe more that I do not think off right now. I can imagine many devs deals with the same scenario. Hence it would be great to use a commit library.Tgo1014
01/11/2023, 3:41 PMTgo1014
01/11/2023, 3:41 PMisError
flagTgo1014
01/11/2023, 3:43 PMTobias Preuss
01/11/2023, 3:48 PMTextInputLayout
automatically shows the character count and max character limit below.Tobias Preuss
01/11/2023, 3:49 PMAle Stamato
01/11/2023, 9:46 PMTobias Preuss
01/12/2023, 3:45 PMoutlinedTextFieldColors
which I use for the OutlinedTextField
also to a Text
defined in supportingText = { ... }
so that error colors and such actually show up?Ale Stamato
01/13/2023, 1:33 PMOutlinedTextField(
colors = TextFieldDefaults.outlinedTextFieldColors(errorSupportingTextColor = Cyan),
value = text,
onValueChange = {
text = it
validate(text)
},
singleLine = true,
label = { Text(if (isError) "Username*" else "Username") },
supportingText = {
Text(
modifier = Modifier.fillMaxWidth(),
text = "Limit: ${text.length}/$charLimit",
textAlign = TextAlign.End,
)
},
isError = isError,
keyboardActions = KeyboardActions { validate(text) })
Thats how you manually set errorSupportingTextColor in OutlinedTextField and its picked up by the Text inside supporting text
If you dont do anything, it shows the default colourTobias Preuss
01/13/2023, 3:19 PMAle Stamato
01/13/2023, 6:06 PMTobias Preuss
01/13/2023, 6:56 PM@ExperimentalMaterial3Api
@Composable
fun outlinedTextColors() = outlinedTextFieldColors(
textColor = baseTextColor(),
unfocusedBorderColor = baseTextColor(),
unfocusedLabelColor = baseTextColor(),
focusedLabelColor = baseTextColor(),
placeholderColor = colorResource(R.color.tertiaryTextColor),
focusedTrailingIconColor = baseIconColor(),
focusedSupportingTextColor = colorResource(R.color.red),
unfocusedSupportingTextColor = colorResource(R.color.red),
disabledSupportingTextColor = colorResource(R.color.red),
errorSupportingTextColor = colorResource(R.color.red),
errorLabelColor = colorResource(R.color.red),
errorBorderColor = colorResource(R.color.red),
)
@ExperimentalMaterial3Api
@Composable
private fun MyOutlinedTextField(
configuration: Configuration,
onTextChanged: (String) -> Unit,
modifier: Modifier = Modifier,
) {
var limitedText by remember { mutableStateOf(configuration.text) }
var reachedTextLimit by remember { mutableStateOf(false) }
val onClearText: () -> Unit = {
limitedText = ""
reachedTextLimit = false
}
OutlinedTextField(
modifier = modifier,
label = { BaseText(configuration.label, subheadlineMedium) },
value = limitedText,
onValueChange = {
reachedTextLimit = it.length + 1 > configuration.textLimit.toInt()
limitedText = if (reachedTextLimit) it.substring(0 until configuration.textLimit.toInt()) else it
onTextChanged(it)
},
textStyle = subheadlineMedium,
colors = outlinedTextColors(),
isError = reachedTextLimit,
singleLine = true,
trailingIcon = {
if (limitedText.isNotEmpty()) {
ForgetIcon { onClearText() }
}
},
supportingText = {
Row {
if (reachedTextLimit && configuration.reachedTextLimitMessage.isNotEmpty()) {
// TODO errorSupportingTextColor is ignored
BaseText(configuration.reachedTextLimitMessage, footnoteMedium, overflow = TextOverflow.Ellipsis)
} else {
BaseText(
modifier = Modifier.fillMaxWidth(),
text = "${limitedText.length} / ${configuration.textLimit}",
textAlign = TextAlign.End,
style = footnoteMedium.copy(
colorResource(R.color.secondaryTextColor)
)
)
}
}
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text, imeAction = ImeAction.Done)
)
}
Ale Stamato
01/14/2023, 6:51 PMTobias Preuss
01/14/2023, 8:20 PMTobias Preuss
01/14/2023, 8:21 PMTobias Preuss
01/16/2023, 1:47 PMBaseText
did overwrite the color. I did not see it immediately.