Skip to content

Instantly share code, notes, and snippets.

@alorma
Created January 19, 2018 09:17
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 alorma/ca98057f884cead2c2e267096ebdd504 to your computer and use it in GitHub Desktop.
Save alorma/ca98057f884cead2c2e267096ebdd504 to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import java.util.HashMap;
/**
* Based on the following Stackoverflow answer:
* http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android
*/
@SuppressWarnings("WeakerAccess")
public class KeyboardUtils implements ViewTreeObserver.OnGlobalLayoutListener {
private final static int KEYBOARD_HEIGHT_DELTA = 200;
private SoftKeyboardToggleListener mCallback;
private View mRootView;
private Boolean prevValue = null;
private float mScreenDensity = 1;
private static HashMap<SoftKeyboardToggleListener, KeyboardUtils> sListenerMap = new HashMap<>();
public interface SoftKeyboardToggleListener {
void onToggleSoftKeyboard(boolean isVisible);
}
@Override
public void onGlobalLayout() {
Rect r = new Rect();
mRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = mRootView.getRootView().getHeight() - (r.bottom - r.top);
float dp = heightDiff / mScreenDensity;
boolean isVisible = dp > KEYBOARD_HEIGHT_DELTA;
if (mCallback != null && (prevValue == null || isVisible != prevValue)) {
prevValue = isVisible;
mCallback.onToggleSoftKeyboard(isVisible);
}
}
/**
* Add a new keyboard listener
*
* @param act calling activity
* @param listener callback
*/
public static void addKeyboardToggleListener(Activity act, SoftKeyboardToggleListener listener) {
removeKeyboardToggleListener(listener);
sListenerMap.put(listener, new KeyboardUtils(act, listener));
}
/**
* Remove a registered listener
*
* @param listener {@link SoftKeyboardToggleListener}
*/
public static void removeKeyboardToggleListener(SoftKeyboardToggleListener listener) {
if (sListenerMap.containsKey(listener)) {
KeyboardUtils k = sListenerMap.get(listener);
k.removeListener();
sListenerMap.remove(listener);
}
}
/**
* Remove all registered keyboard listeners
*/
public static void removeAllKeyboardToggleListeners() {
for (SoftKeyboardToggleListener l : sListenerMap.keySet())
sListenerMap.get(l).removeListener();
sListenerMap.clear();
}
/**
* Manually toggle soft keyboard visibility
*
* @param context calling context
*/
public static void toggleKeyboardVisibility(Context context) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
/**
* Force closes the soft keyboard
*
* @param activeView the view with the keyboard focus
*/
public static void forceCloseKeyboard(View activeView) {
InputMethodManager inputMethodManager = (InputMethodManager) activeView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activeView.getWindowToken(), 0);
}
private void removeListener() {
mCallback = null;
mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
private KeyboardUtils(Activity act, SoftKeyboardToggleListener listener) {
mCallback = listener;
mRootView = ((ViewGroup) act.findViewById(android.R.id.content)).getChildAt(0);
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
mScreenDensity = act.getResources().getDisplayMetrics().density;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment