2

How can I limit the size of input of TextField? in XML I used InputFilter something like this:

val limitSizeInputFilter = arrayOf<InputFilter>(ByteLengthFilter(100, "UTF-8"))

binding.nsetName.filters = limitSizeInputFilter

Is there any equivalent to it in compose?

I found this but this restrict to regex which is depends on characters. And I need to check the byte length.

c-an
  • 3,543
  • 5
  • 35
  • 82
  • 1
    `inputString.toByteArray(charset("UTF-8")).size` will give the length of string correct ? – J.K Mar 24 '23 at 05:25
  • Yeah, I thought there might be simpler solution. But I just counted all the bytes and then added condition in the onValueChange. – c-an Mar 24 '23 at 17:22
  • Thats all the solution, you already able to find it, thats why i didnt post :) – J.K Mar 25 '23 at 02:58

1 Answers1

0

on TextField has the method onValueChange. You can here put any condition to filter input value. Even you can use as inputFilter for symbols with regex and so on.

Your question sulition here:

var text by remember {
    mutableStateOf("")
}

NTextField(
    value = text,
    onValueChange = {
        if (it.length < 100){
            text = it
        }
    },
)