Friday, December 21, 2012

Easy way to check if soft keyboard is open

Here's the really easy way to handle very specific 'soft' keyboard events: opening and closing.
final RelativeLayout view = (RelativeLayout)findViewById(R.id.activity_loading);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
        public void onGlobalLayout() {
            if ((view.getRootView().getHeight() - view.getHeight()) >    
                    view.getRootView().getHeight()/3) {
            
  // keyboard is open

            } else {
             
  // keyboard is closed
            
     }
        }
    });

5 comments: