Saturday, August 3, 2013

Detect Software Keyboard is Visible on Android Device

Solution 1:

  View activityRootView = fragmentView.findViewById(R.id.root_view);
   activityRootView.getViewTreeObserver().addOnGlobalLayoutListener( new                      OnGlobalLayoutListener() {
   @Override
    public void onGlobalLayout() {

 Rect r = new Rect();
        activityRootView.getWindowVisibleDisplayFrame(r);
 
        int screenHeight =  activityRootView.getRootView().getHeight();
Log.e("screenHeight",String.valueOf(screenHeight));
        int heightDiff = screenHeight - (r.bottom - r.top);
        Log.e("heightDiff",String.valueOf(heightDiff));
boolean visible = heightDiff > screenHeight / 3;
   
 Log.e("visible",String.valueOf(visible));

if(visible)
                          { if true ,do your stuff here !}
else
                           { else, do your stuff here }

}
});


Solution 2:

View activityRootView = fragmentView.findViewById(R.id.root_view);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() -                                                                                                                                      activityRootView.getHeight();
if (heightDiff > 150) 
{ // if more than 150 pixels, its probably a keyboard...
do your stuff , if keyboard visible
}
else 
{

}
}

});

1 comment:

  1. I Need Send SMS BY ONE SIMCARD, IF YOU CAN PLEASE CONTACT ME mayihelpu789@gmail.com

    ReplyDelete