maxLength
Returns [InputTransformation] that rejects input which causes the total length of the text field
maxLength
Function
Common
fun InputTransformation.maxLength(maxLength: Int): InputTransformation
Returns InputTransformation that rejects input which causes the total length of the text field
to be more than maxLength characters.
This transformation also sets the maximum text length for accessibility services. When using an
OutputTransformation that adds decorating characters, the announced maximum length may need to
be adjusted to account for those characters. See the linked sample for an example of a custom
maxLength filter that does this.
Code Examples
BasicTextFieldInputTransformationMaxLengthCustom
@Composable
fun BasicTextFieldInputTransformationMaxLengthCustom() {
val state = remember { TextFieldState() }
BasicTextField(
state,
inputTransformation =
object : InputTransformation {
override fun SemanticsPropertyReceiver.applySemantics() {
maxLength(14)
}
override fun TextFieldBuffer.transformInput() {
if (length > 10) revertAllChanges()
}
},
outputTransformation =
OutputTransformation {
if (length > 0) insert(0, "(")
if (length > 4) insert(4, ") ")
if (length > 9) insert(9, "-")
},
)
}