Android external keyboard detection

3,888 views
Skip to first unread message

cmat

unread,
Apr 23, 2012, 5:09:16 PM4/23/12
to android-platform
Hi,
We have built a native android app where user has to fill a form
containing editText boxes, buttons etc. My team has been trying to
find ways to supress the soft keyboard on Smasung Galaxy tablets when
an external keyboard is connected. There seems to be no easy way to
accomplish this.
The problem we are facing is two fold
1) How to detect an external keyboard?
configChanges, keyboard everything has been tried and nothing works.
configuration.keyboard always shows value 1== KEYBOARD_NOKEYS.

2) How to suppress the soft keyboard?
The perfect way is to have something global that suppresses soft
keyboard when external keyboard is connected. But tried doing it on
start of an activity but as soon as the editbox is tapped the soft
keyboard pops up even if the external keyboard is still connected.
However this doesn't happen on the native android browser. If i click
on an input field on a form on the browser, soft keyboard doesnt come
up unless i disconnect external keyboard. So there does seem to be a
cleaner way to do this. Browsing the android browser source code to
see how its done natively didn't yield much!

A tedious way seems to have ontouch listeners on every text field and
hide the soft keyboard. Since our form spans many pages this is a lot
of work that we need to do.

Help is appreciated.

Dianne Hackborn

unread,
Apr 23, 2012, 10:13:41 PM4/23/12
to android-...@googlegroups.com
The device software needs to do this.  If it isn't doing it, then that is a policy the device has.  You can't override it.

That said, it is surprising that you are seeing this behavior.  It has always been the case on Android that the presence of a physical keyboard means the IME stays hidden.  Maybe your device is not correctly detecting your keyboard?


--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

cmat

unread,
Apr 24, 2012, 1:37:49 PM4/24/12
to android-platform
I think the device is detecting it. Because when i load an html form
on the android native browser, even if i tap on a text field, the soft
key board stays hidden when connected to external keyboard. But the
app which we have written has a form with Text fields and tap on that
pops up the soft keyboard. So is it something to do with the way we
have built our app? Or could it be that the EditText always does this
and the native android browser uses some other input field type..?
> hack...@android.com

Chris Stratton

unread,
Apr 24, 2012, 3:10:28 PM4/24/12
to android-...@googlegroups.com
On Tuesday, April 24, 2012 1:37:49 PM UTC-4, cmat wrote:
I think the device is detecting it. Because when i load an html form
on the android native browser, even if i tap on a text field, the soft
key board stays hidden when connected to external keyboard. But the
app which we have written has a form with Text fields and tap on that
pops up the soft keyboard. So is it something to do with the way we
have built our app? Or could it be that the EditText always does this
and the native android browser uses some other input field type..?

I do not recall what personally happened the one time I briefly had a keyboard connected to a tablet, but it's my recollection that early adopters of bluetooth keyboards and people playing with usb-host-mode hacks (xda-developers threads, etc) tended to run into this problem a lot, to the point where someone put soft keyboard IME on the market that display an essentially zero-height keyboard as a workaround.

Dianne Hackborn

unread,
Apr 24, 2012, 5:10:21 PM4/24/12
to android-...@googlegroups.com
This is the default framework implementation of InputMethodService to determine whether to show the IME:

    /**
     * Override this to control when the soft input area should be shown to
     * the user.  The default implementation only shows the input view when
     * there is no hard keyboard or the keyboard is hidden.  If you change what
     * this returns, you will need to call {@link #updateInputViewShown()}
     * yourself whenever the returned value may have changed to have it
     * re-evaluated and applied.
     */
    public boolean onEvaluateInputViewShown() {
        Configuration config = getResources().getConfiguration();
        return config.keyboard == Configuration.KEYBOARD_NOKEYS
                || config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES;
    }

The default framework implementation to respond to a request to show the IME is this:

    /**
     * The system has decided that it may be time to show your input method.
     * This is called due to a corresponding call to your
     * {@link InputMethod#showSoftInput InputMethod.showSoftInput()}
     * method.  The default implementation uses
     * {@link #onEvaluateInputViewShown()}, {@link #onEvaluateFullscreenMode()},
     * and the current configuration to decide whether the input view should
     * be shown at this point.
     * 
     * @param flags Provides additional information about the show request,
     * as per {@link InputMethod#showSoftInput InputMethod.showSoftInput()}.
     * @param configChange This is true if we are re-showing due to a
     * configuration change.
     * @return Returns true to indicate that the window should be shown.
     */
    public boolean onShowInputRequested(int flags, boolean configChange) {
        if (!onEvaluateInputViewShown()) {
            return false;
        }
        if ((flags&InputMethod.SHOW_EXPLICIT) == 0) {
            if (!configChange && onEvaluateFullscreenMode()) {
                // Don't show if this is not explicitly requested by the user and
                // the input method is fullscreen.  That would be too disruptive.
                // However, we skip this change for a config change, since if
                // the IME is already shown we do want to go into fullscreen
                // mode at this point.
                return false;
            }
            Configuration config = getResources().getConfiguration();
            if (config.keyboard != Configuration.KEYBOARD_NOKEYS) {
                // And if the device has a hard keyboard, even if it is
                // currently hidden, don't show the input method implicitly.
                // These kinds of devices don't need it that much.
                return false;
            }
        }
        if ((flags&InputMethod.SHOW_FORCED) != 0) {
            mShowInputForced = true;
        }
        return true;
    }

So for the default behavior, the IME should never be shown if the configuration indicates that a hard keyboard is attached.

Maybe the IME you are using has overridden this to have some other behavior.

--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-platform/-/Xo2uk9Bae80J.

To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.



--
Dianne Hackborn
Android framework engineer

cmat

unread,
Apr 26, 2012, 12:24:25 PM4/26/12
to android-platform
Thanks Dianne.
I tried logging the values for Configuration and this is what i get
when hardkeyboard is connected or not! And we have tried with many
keyboards and tablets and the behavior is the same
getResources().getConfiguration().hardKeyboardHidden value is 2
(HARDKEYBOARDHIDDEN_YES)
getResources().getConfiguration().keyboard value is 1
(KEYBOARD_NOKEYS)

We are using Android 3.1 (API 12). I tried with API 15 as well, but
same output. Shouldnt the values change based on soft vs hard
keyboard?
> hack...@android.com
Reply all
Reply to author
Forward
0 new messages