Meirion
Meirion

Reputation: 788

trigger.io - close soft keyboard?

Is there a way to programatically close the Android on screen keyboard with trigger.io?

I have a search field with an auto-complete search on. When the user stops typing I show the search results, but the onscreen keyboard stays visible - obscuring a number of the results.

Upvotes: 2

Views: 1340

Answers (1)

Patrick Rudolph
Patrick Rudolph

Reputation: 2241

There are actually two ways of doing this using Trigger.io:

Native plugin

You can write a native plugin that hides the soft keyboard. The relevant code should look something like this (based on Close/hide the Android Soft Keyboard):

InputMethodManager inputMethodManager = (InputMethodManager) ForgeApp.getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(ForgeApp.getActivity().getCurrentFocus().getWindowToken(), 0);

Javascript

The proper way to hide the soft keyboard using Javascript would be to blur the element that is currently focused. In modern browsers, you just need to call:

document.activeElement.blur()

However, document.activeElement is not always available and sometimes seems to be incorrect. I use something like this:

if (document.activeElement && 
    document.activeElement.blur && 
    document.activeElement !== document.body) {
  document.activeElement.blur();
}
else {
  jQuery(':focus').blur();
}

Even this will not work for some Android 2.x devices. Check out How can I hide the Android keyboard using JavaScript? for more workarounds.

Upvotes: 7

Related Questions