-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added additional capibilities #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ public class KeyboardUtils implements ViewTreeObserver.OnGlobalLayoutListener | |
|
||
private SoftKeyboardToggleListener mCallback; | ||
private View mRootView; | ||
private Boolean prevValue = null; | ||
public static Boolean sPrevValue = null; | ||
private float mScreenDensity = 1; | ||
private static HashMap<SoftKeyboardToggleListener, KeyboardUtils> sListenerMap = new HashMap<>(); | ||
|
||
|
@@ -41,12 +41,20 @@ public void onGlobalLayout() | |
float dp = heightDiff/ mScreenDensity; | ||
boolean isVisible = dp > MAGIC_NUMBER; | ||
|
||
if (mCallback != null && (prevValue == null || isVisible != prevValue)) { | ||
prevValue = isVisible; | ||
if (mCallback != null && (sPrevValue == null || isVisible != sPrevValue)) { | ||
sPrevValue = isVisible; | ||
mCallback.onToggleSoftKeyboard(isVisible); | ||
} | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we can do it this way. What if there are multiple listeners? We don't want to share the same static variable that tracks the previous value for all of them, do we? I think the better way of doing this, would be to return a handle to the KeyBoardUtils object when adding the listener and then calling a non-static method on that object to get the current value. |
||
* Keyboard visibility getter | ||
* @return whether the keyboard is shown or not. | ||
*/ | ||
public static boolean isKeyboardShown() { | ||
return sPrevValue; | ||
} | ||
|
||
/** | ||
* Add a new keyboard listener | ||
* @param act calling activity | ||
|
@@ -105,6 +113,36 @@ public static void forceCloseKeyboard(View activeView) | |
inputMethodManager.hideSoftInputFromWindow(activeView.getWindowToken(), 0); | ||
} | ||
|
||
/** | ||
* Closes soft keyboard | ||
* @param context is the context where the keyboard was open from | ||
*/ | ||
public static void closeSoftKeyboard(Context context) { | ||
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); | ||
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); | ||
} | ||
|
||
/** | ||
* Opens soft keyboard | ||
* @param context is the context where the keyboard was open from | ||
*/ | ||
public static void openSoftKeyboard(Context context) { | ||
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); | ||
imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we not using "InputhMethodManager.showSoftInput(View, int) ? |
||
} | ||
|
||
/** | ||
* Force close keyboard which got open unnaturally using Activity | ||
* @param activity is the activity where the keyboard was open from | ||
*/ | ||
public static void forceCloseSoftKeyboardByActivity(Activity activity) { | ||
InputMethodManager inputManager = (InputMethodManager) | ||
activity.getSystemService(Context.INPUT_METHOD_SERVICE); | ||
inputManager.hideSoftInputFromWindow((null == activity.getCurrentFocus()) ? | ||
null : activity.getCurrentFocus().getWindowToken(), | ||
InputMethodManager.HIDE_NOT_ALWAYS); | ||
} | ||
|
||
private void removeListener() | ||
{ | ||
mCallback = null; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, why does this have to be public and static? This should not be accessed from outside.