try below code:-
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView()
.getHeight() - (r.bottom - r.top);
if (lastDiff == heightDiff)
return;
lastDiff = heightDiff;
Log.i("aerfin","arefin "+lastDiff);
if (heightDiff > 100) { // if more than 100 pixels, its
// probably a keyboard...
flag2 = 0;
} else {
if (flag == false)
flag2 = 1;
}
}
});
more info see below link least use full for me:-
Maintain keyboard open/closed state for EditText when app comes to foreground