Reputation: 39731
I'd like to get notified when the virtual keyboard is shown / dismissed. This does not seem to be possible, other than by using some layout resizing listener tricks:
How to check visibility of software keyboard in Android?
My activity has a single EditText. I could make it not have focus at activity startup, then add a focuschangelistener to it. When it gains focus, I can do my onVirtualKeyboardShown() stuff. If I could just listen for the back key being pressed in the EditText, I could then interpret that as the virtual keyboard being hidden. Something like:
EditText et = ...;
et.setOnFocusChangedListener(new OnFocusChangedListener() {
public void onFocusChanged(boolean focused) {
if (focused) {
// virtual keyboard probably showing.
}
}
});
et.setKeyListener(new KeyListener() {
public void onKeyPressed(int code) {
if (code == BACK_KEY) [
if (et.isFocused()) {
// virtual keyboard probably hiding.
// lose focus to set up for next time.
et.setFocused(false);
}
}
}
});
Seems like any approach is fraught with problems given all the differences between virtual keyboards, then we also have to deal with physical keyboards too,
Thanks
Upvotes: 2
Views: 3722
Reputation: 2317
// Catch the keyboard height
final LinearLayout masterView = (LinearLayout) findViewById(R.id.conversation_prent);
masterView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
Rect r = new Rect();
masterView.getWindowVisibleDisplayFrame(r);
int result = 0;
int resourceId = getResources().getIdentifier(
"status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(
resourceId);
}
int heightDiff = masterView.getRootView().getHeight()
- masterView.getHeight();
heightDiff = heightDiff - (ab.getHeight() + result);
Log.e("Keyboard Size", "Size: " + heightDiff);
if (heightDiff > 200) {
// The keyboard is shown
} else {
// The keyboard is hidden
}
}
});
if your app is running on android < 3 (HoneyComb) delete the parts of the code that are related to the actionbar.
Upvotes: 1
Reputation: 1007584
This does not seem to be possible, other than by using some layout resizing listener tricks
Correct.
I want to be notified so I can show my own suggestions ribbon above the virtual keyboard.
Not all Android devices use virtual keyboards. Some have physical keyboards. Since you need to support both types of devices, you need to come up with a UI design that does not assume that everyone has a virtual keyboard.
Upvotes: 2