How do we find out that the keyboard is open? Android | Java
I’m going to teach a KeyboardListener to alert when the keyboard opens. For example, sometimes you want to hidden view when the keyboard opens so in this case, you should know when keyboard open In the following I will teach you how to Build an OnKeyboardShowListener:
Step 1: Get The Root Of The View
First of all, you need to get the root, you can access it from the activity so I create a function and put the activity in parameter, with android.R.id.content you can access the root without knowing its id:
public static void OnkeyBoardShowListener(final Activity activity) {final View activityRootView =
activity.findViewById(android.R.id.content);}
now we have the root in activityRootView.
Step 2: Measure the Height
How can we get the height?!
If you use mainLayout.getRootView (). getHeight() it return 0px because it’s not set yet, ViewTreeObserver is used to register listeners that can be notified of global changes in the view tree, To make things a bit simpler I’ll give you an example :
notify you if the view is hidden or appear.
if the height of width changes it will notify you. (That’s what we want)
getViewTreeObserver().addOnGlobalLayoutListener wait for the View to be measured, displayed, and then take its width and height.
public static void OnkeyBoardShowListener(final Activity activity) { final View activityRootView = activity.findViewById(android.R.id.content); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() { }
});
}
now We can take and use Root Height and Width but the problem is how to figure out if the keyboard is open?
Step 3: Visible Display Size
getWindowVisibleDisplayFrame(Rect rect): Retrieve the overall visible display size in which the window this view is attached to has been positioned.
public static void OnkeyBoardShowListener(final Activity activity) { final View activityRootView = activity.findViewById(android.R.id.content); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
activityRootView.getWindowVisibleDisplayFrame(rect);
}
});
}
We need to measure the height of the Root and use the Rect to measure the difference between the height and the rect when the keyboard open it almost becomes noticeable.
public static void OnkeyBoardShowListener(final Activity activity) { final View activityRootView = activity.findViewById(android.R.id.content); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
activityRootView.getWindowVisibleDisplayFrame(rect);
int heightRoot = activityRootView.getRootView().getHeight();
int heightDiff = heightRoot - rect.bottom;
}
});
}
From now on it depends on your usage what you want to use Note that heightRoot and heightDiff give you px output so:
public static void OnkeyBoardShowListener(final Activity activity) { final View activityRootView = activity.findViewById(android.R.id.content); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
activityRootView.getWindowVisibleDisplayFrame(rect);
int heightRoot = activityRootView.getRootView().getHeight();
int heightDiff = heightRoot - rect.bottom; if (heightDiff > dpToPx(activity, 200)) {
//Keyboard is open
}else if (heightDiff < dpToPx(activity, 200)) {
//Keyboard is close
}
}
});
}
Step 4: convert Dp to Px
private static float dpToPx(Context context, int dp) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}
Now You Are New Generation Wizard. :)
You Can Read My other article :
We are not Coding the program, we are Coding Future.
IG: www.instagram.com/behnamnasehi
Gmail: behnammnasehi@gmail.com
Telegram: @Behnamnasehii