1

I'm trying to make my TextField accept only characters using keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text) as:

      TextField(
                value = query3.value,
                onValueChange = { newValue ->
                    query3.value = newValue
                },
                singleLine = true,
                label = {
                    Text(
                        "Bank name",
                        color = colorResource(id = R.color.bright_green),
                        fontFamily = FontFamily(Font(R.font.poppins_regular)),
                        fontSize = with(LocalDensity.current) { dimensionResource(id = 
                        R.dimen._12ssp).toSp() },
                        )
                },
                interactionSource = interactionSource,
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
                 .
                 .
                 .

But the soft keyboard still allows entering numbers. How do I make the soft keyboard allow entering ONLY characters?

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Sparsh Dutta
  • 2,450
  • 4
  • 27
  • 54

3 Answers3

6

You will need to filter out digits on your text field callback,

TextField(
            value = query3.value,
            onValueChange = { newValue ->
                query3.value = newValue.filter { !it.isDigit() }
            },
            singleLine = true,
Francesc
  • 25,014
  • 10
  • 66
  • 84
  • 1
    It should be noted that while this works, it will fail if you place a breakpoint on the line of code inside of onValueChange. This seems to be some quirky bug in Compose during debugging and cost me hours to determine that the code actually was working when no breakpoints were present. – Johann Jan 31 '22 at 15:01
1

I'm afraid this functionality is not supported in Android. Yes if you want the user to be able to input only a specified set of characters, then filtering it out in the onValueChange or using filters would be the correct choice. See because, in a scenario where a user is only allowed a to enter numbers, the keyboard will still (always) show the star and (perhaps) the plus key, even though they are not entered upon clicking them. If you want something like a keyboard with only specific digits, I'm afraid you'll have to design your own keyboard (in-app), which is not something very difficult to achieve with Compose. It's a piece o' cake

Richard Onslow Roper
  • 5,477
  • 2
  • 11
  • 42
  • Hey by the way if you are using something like ```var query3 = mutableStateOf ("")```, I think you should use the syntactic sugar as ```var query3 by mutableStateOf ("")```. You see if you use this, everything else remains the same. Recompositions are triggered. You just don't need to write ```.value``` everytime and can use the state-holder like a regular variable. – Richard Onslow Roper Jul 22 '21 at 18:05
  • The ```by``` keyword helps treat any compose state as the state type. This is true at least for the primitive types, and I think for custom types, you will need to define some delegates – Richard Onslow Roper Jul 22 '21 at 18:17
0
usersAnswer.value = it.filter{ it.isLetter() || it.isWhitespace() }.lowercase()
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37