Skip to content

Instantly share code, notes, and snippets.

@CarlosACepeda
Forked from lopspower/KeyboardUtils.java
Created October 24, 2019 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlosACepeda/587e699a42370c5fb507867aa106520a to your computer and use it in GitHub Desktop.
Save CarlosACepeda/587e699a42370c5fb507867aa106520a to your computer and use it in GitHub Desktop.
Force Hide Keyboard Android

Force Hide Keyboard

Twitter

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your focused view.

public static void hideKeyboard(Activity activity) {
    View view = activity.findViewById(android.R.id.content);
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.HIDE_IMPLICIT_ONLY as the second parameter to ensure you only hide the keyboard when the user didn't explicitly force it to appear (by holding down menu).


📚 Best Android Gists

You can see other best Android Gists or offer your just here https://github.com/lopspower/BestAndroidGists 👍.

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
public class KeyboardUtils {
public static void hideKeyboard(Activity activity) {
View view = activity.findViewById(android.R.id.content);
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
public static void showKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public static void addKeyboardVisibilityListener(View rootLayout, OnKeyboardVisibiltyListener onKeyboardVisibiltyListener) {
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
Rect r = new Rect();
rootLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = rootLayout.getRootView().getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
boolean isVisible = keypadHeight > screenHeight * 0.15; // 0.15 ratio is perhaps enough to determine keypad height.
onKeyboardVisibiltyListener.onVisibilityChange(isVisible);
});
}
public interface OnKeyboardVisibiltyListener {
void onVisibilityChange(boolean isVisible);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment