I would like to detect if the current users phone has a hardware keyboard or only a on-screen keyboard. Is this possible with the SDK?
getConfiguration(). keyboard are a good way of checking which keyboard (if any) is available. Yes, if for your purposes you are counting a "12-key keyboard" as a "hardware keyboard".
Android provides no direct way to determine if the keyboard is open, so we have to get a little creative. The View class has a handy method called getWindowVisibleDisplayFrame from which we can retrieve a rectangle which contains the portion of the view visible to the user.
To be able to open it anywhere, you go into the settings for the keyboard and check the box for 'permanent notification'. It will then keep an entry in the notifications which you can tap to bring up the keyboard at any point.
Yes, you can.
Fetch the Configuration object using
Configuration config = getResources().getConfiguration();
...and then look at the keyboard
field.
If they value of keyboard
is not KEYBOARD_NOKEYS, the user has a hardware keyboard.
Note that as @Carl says in his comment below, the user may attach a USB keyboard at any point while your app is running, causing the value of keyboard
to change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With