Android EditText, soft keyboard show/hide event? Android EditText, soft keyboard show/hide event? android android

Android EditText, soft keyboard show/hide event?


Hi I'd used following workaround:

As far as my content view is a subclass of LinearLayout (could be any other view or view group), I'd overridden onMeasure method lilke following:

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);    final int actualHeight = getHeight();    if (actualHeight > proposedheight){        // Keyboard is shown    } else {        // Keyboard is hidden    }    super.onMeasure(widthMeasureSpec, heightMeasureSpec);}

This workaround helped me to hide some controls when keyboard is showing and bring back otherwise.

Hope this would be useful.


There actually isn't such an event to catch. The IME is simply showing and hiding its window; the feedback you get from this is the window manager causing your own window's content to resize if you have put it in resize mode.


I solved this issue by using onGlobalLayoutListener :

 final View activityRootView = findViewById(R.id.top_root);        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {            public void onGlobalLayout() {                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();                if (heightDiff > 100) {                    // keyboard is up                } else {                    // keyboard is down                }            }        });

Here activityRootView is your Activity's root view.