2

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

Community
  • 1
  • 1
user291701
  • 38,411
  • 72
  • 187
  • 285
  • Why do you want to be notified when the virtual keyboard is shown/dismissed? Are you trying to replace the keyboard with your own virtual keyboard? – Eric Brynsvold Oct 28 '11 at 16:07
  • No, I want to be notified so I can show my own suggestions ribbon above the virtual keyboard. When the virtual keyboard is dismissed, I'd like to hide my ribbon. (it's just a horizontal scrollview) – user291701 Oct 28 '11 at 16:09
  • I don't know of a way to do that - you might be better off using an AutoComplete on your edit texts if you want to provide suggestions - at least according to [this post](http://stackoverflow.com/questions/2531253/how-do-i-add-words-to-the-suggestions-on-top-of-the-soft-keyboard), especially if you want the suggestions to appear when you're working with physical keyboards...or you just only show the suggestions when the edit text has focus and hide them when it loses focus. – Eric Brynsvold Oct 28 '11 at 16:16
  • Yeah the AutoComplete would be better but it just doesn't make sense for this context. I'm showing an EditText and want to show suggested twitter handles as the user is typing. Would be nice to show suggestions only when the user actually has the virtual keyboard opened. I know this doesn't work in all cases, like the droid pro has a physical keyboard that is always "open" I think. Argh, android. – user291701 Oct 28 '11 at 16:29

2 Answers2

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1
    // 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.

Ali Alnoaimi
  • 2,278
  • 2
  • 21
  • 31