How To Check If The Software Keyboard Is Shown In AndroidPosted by Roger Keays, 21 February 2012, 10:11 AM |
Here is a method to detect if the soft keyboard is visible on the screen in Android. All the other methods I have seen test the height of screen elements to guess whether it is displayed. This doesn't work for keyboards like WifiKeyboard which is an invisible keyboard.
This method uses the callback result of InputMethodManager.showSoftInput() to determine if the keyboard status changed. This is suitable for me because I need to call showSoftInput() anyway if the keyboard is not shown. The result from the operation needs to be polled because it is asynchronous. This example only polls 500 milliseconds and assumes that anything longer than that is caused by the keyboard being loaded and rendered.
Here is how the code is used:
// try to show the keyboard and capture the result
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
IMMResult result = new IMMResult();
imm.showSoftInput(editText, 0, result);
// if keyboard doesn't change, handle the keypress
int res = result.getResult();
if (res == InputMethodManager.RESULT_UNCHANGED_SHOWN ||
res == InputMethodManager.RESULT_UNCHANGED_HIDDEN) {
showTags();
}
The IMMResult class is the key element. It looks like this:
/**
* To capture the result of IMM hide/show soft keyboard
*/
private class IMMResult extends ResultReceiver {
public int result = -1;
public IMMResult() {
super(null);
}
@Override
public void onReceiveResult(int r, Bundle data) {
result = r;
}
// poll result value for up to 500 milliseconds
public int getResult() {
try {
int sleep = 0;
while (result == -1 && sleep < 500) {
Thread.sleep(100);
sleep += 100;
}
} catch (InterruptedException e) {
Log.e("IMMResult", e.getMessage());
}
return result;
}
}
The useful result values are:
-
InputMethodManager.RESULT_UNCHANGED_SHOWN
-
InputMethodManager.RESULT_UNCHANGED_HIDDEN
-
InputMethodManager.RESULT_SHOWN
- InputMethodManager.RESULT_HIDDEN
See the InputMethodManager documentation for details.
Hope this helps you!
| << How To Show Negative Numbers Using Brackets With NumberFormatter In Java | Back to Blog | How To Move A Node In Nested Sets With SQL >> |
Comment posted by: Ruslan Popov on 19/03/2012 7:00:22 AM
Just don't understand how to integrate this with Activity. Can you comment this more?
Comment posted by: Roger Keays on 19/03/2012 8:24:37 AM
- Put the IMMResult private class in your Activity (or in a separate file if you need to reuse it).
- Put the first code block wherever you need conditional logic based on the state of the keyboard. This code block first shows the keyboard then checks the IMMResult to find out if it was hidden beforehand or not. You could change it to first hide the keyboard then query the result if that works better for you.
It's quite a mess really, but it should give you the information you need.
Missing API features FTW.
Comment posted by: Ruslan Popov on 19/03/2012 5:21:48 PM
I don't need to show the keyboard manually. I just want to detect when it changes its state (shown/hidden) with appropriate callback function. Does it possible?