https://kotlinlang.org logo
#compose-android
Title
# compose-android
a

Abhimanyu

10/21/2023, 5:55 AM
Hi, How to find the type of Keyboard opened in Jetpack Compose? More details in 🧵
I have a weird bug 🐛 where the keyboard opened is sometimes not the expected type. (Expecting number but alphabets are show and vice versa).
As this is not a consistently reproducible issue. I am trying to add logs to find more info about this. Like, when a keyboard is opened, will observe keyboard changes using this code (https://stackoverflow.com/a/73640325/9636037) and log the
TextField
causing the keyboard to open and the type of keyboard shown.
This help to narrow down the issue to device specific, or and any other info. To do this, how do I find the opened keyboard type? I need to differentiate these keyboards programatically for logging and debugging.
c

Chrimaeon

10/21/2023, 8:01 AM
1. You can only “request” a type of keyboard. Which keyboard is shown, is up to the OS. 2. How would you handle hardware keyboards? 3. What about custom keyboards? 4. your question is not really Kotlin or compose specific.
a

Abhimanyu

10/21/2023, 8:11 AM
Hi @Chrimaeon, Thanks for responding. Regarding Hardware Keyboards, I do have validation on the input which handles the input. (sanitization and validation). I do understand that we can only request the OS to show a specific type of keyboard. Hence, my question, how to check what keyboard is show when I request for a specific keyboard. For example, in my case, I am requesting for
keyboardType = KeyboardType.NumberPassword,
, but I am seeing Keyboard with Text,
Also, the focused TextField is based on few conditions. So, when the screen opens, a TextField is focused based on some business criteria. It would also be used to find which TextField is focused from UI layer (Jetpack Compose)
c

Chrimaeon

10/21/2023, 8:14 AM
What I actually meant is, there is no „keyboard type“ that you can check. Validating the chars coming to your Textfield is the only check you can do.
a

Abhimanyu

10/21/2023, 8:15 AM
This is the issue I am facing I don’t see any focused TextField here and my expectation is that the first TextField should be focused and show
NumberPassword
keyboard.
c

Chrimaeon

10/21/2023, 8:16 AM
I think there is something like „focusrequester“. Maybe this helps with your issue on finding out which textfield is focused.
a

Abhimanyu

10/21/2023, 8:17 AM
I am using
FocusRequestor
to request focus. Can you please share how to find which
TextField
is focused?
c

Chrimaeon

10/21/2023, 8:18 AM
Not sure, was just an idea. But Google might be able to help 😉
a

Abhimanyu

10/21/2023, 8:18 AM
Not getting any idea on how to do this from Google. 😓
v

vide

10/21/2023, 11:11 AM
You probably should just be focusing the first field, it should open up a keyboard automatically. Can you set a numeric type for the input field?
There is a bug with requesting keyboards without focus to any input element: https://issuetracker.google.com/issues/301477279
a

Abhimanyu

10/21/2023, 11:15 AM
Oh. There is an open bug 🐞. I am setting the keyboard type as required. For showing the keyboard, I am requesting focus as well as using show(). Will try removing the explicit show and see if that works.
z

Zach Klippenstein (he/him) [MOD]

10/23/2023, 3:20 AM
If the keyboard type isnt what you specified on the text field and the keyboard you’re using does in fact support that type, please file a bug.
a

Abhimanyu

10/23/2023, 5:15 AM
The issue seems to be the one @vide has shared. Thanks for sharing Elias, removing
keyboardController.show()
seems to fix the issue.
z

Zach Klippenstein (he/him) [MOD]

10/23/2023, 4:30 PM
That looked like a different issue than what you described, but yea calling
show
yourself has undefined behavior
2 Views