Search code examples
androidandroid-jetpack-composeandroid-jetpack-compose-textandroid-compose-textfield

How to set the inputType for a TextField as a binary (0 and 1) in Jetpack Compose?


I'm trying to set input type of the TextField as a Binary but there is no KeyboardOptions KeyboardType as Binary.

So how can I accomplish this ?

  TextField(
        value = text,
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
        onValueChange = {
            text = it
        },
        label = { Text("Enter Binary") }
    )

Solution

  • There is no such input options, but you can filter invalid values out:

    TextField(
        value = text,
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
        onValueChange = {
            text = it.filter { it == '0' || it == '1' }
        },
        label = { Text("Enter Binary") }
    )