Frage

Want to Toast a text everytime when soft keyboard state changes from shown to hidden. Here I just want to getText() from EditText and everytime I click on EditText the soft Keyboard must open and after pressing back or return the text must be shown as Toast

Thanks in Advance

War es hilfreich?

Lösung

There is no direct listener for keyboard state detection so you need some programatic implementation as below

private boolean wasKeyboardOpen = false;

try {
        activityMainView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Rect r = new Rect();
                activityMainView.getWindowVisibleDisplayFrame(r);

                int heightDiff = activityMainView.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > 100) {
                    wasKeyboardOpen = true;
                    // kEYBOARD IS OPEN

                } else {
                    if (wasKeyboardOpen) {
                        wasKeyboardOpen = false;
                        // Do your toast here

                    }
                    // kEYBOARD IS HIDDEN
                }
            }
        });
    } catch (Throwable e) {
        e.printStackTrace();
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top