diff --git a/.idea/misc.xml b/.idea/misc.xml index 1d6606a..b0a270f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,32 @@ + + + + + + + diff --git a/KeyboardUtils.java b/KeyboardUtils.java deleted file mode 100644 index 33b94aa..0000000 --- a/KeyboardUtils.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.ravindu1024.utils; - -import android.app.Activity; -import android.graphics.Rect; -import android.view.View; -import android.view.ViewGroup; -import android.view.ViewTreeObserver; -import android.content.Context; - -import java.util.HashMap; - -/** - * Based on the following Stackoverflow answer: - * http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android - */ -public class KeyboardUtils implements ViewTreeObserver.OnGlobalLayoutListener -{ - private final static int MAGIC_NUMBER = 200; - - private SoftKeyboardToggleListener mCallback; - private View mRootView; - private Boolean prevValue = null; - private float mScreenDensity = 1; - private static HashMap 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 > MAGIC_NUMBER; - - if (mCallback != null && (prevValue == null || isVisible != prevValue)) { - prevValue = isVisible; - mCallback.onToggleSoftKeyboard(isVisible); - } - } - - - public static void addKeyboardToggleListener(Activity act, SoftKeyboardToggleListener listener) - { - removeKeyboardToggleListener(listener); - - sListenerMap.put(listener, new KeyboardUtils(act, listener)); - } - - public static void removeKeyboardToggleListener(SoftKeyboardToggleListener listener) - { - if(sListenerMap.containsKey(listener)) - { - KeyboardUtils k = sListenerMap.get(listener); - k.removeListener(); - - sListenerMap.remove(listener); - } - } - - public static void removeAllKeyboardToggleListeners() - { - for(SoftKeyboardToggleListener l : sListenerMap.keySet()) - sListenerMap.get(l).removeListener(); - - sListenerMap.clear(); - } - - public static void toggleKeyboardVisibility(Context context) - { - InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); - inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } - - /** - * Closes the 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; - } - -}