public class KeyboardWatcher {
private final WeakReference<Activity> activityRef;
private WeakReference<View> rootViewRef;
private WeakReference<OnKeyboardToggleListener> onKeyboardToggleListenerRef;
private ViewTreeObserver.OnGlobalLayoutListener viewTreeObserverListener;
/** * Instantiates a new Keyboard watcher. * * @param activity the activity */ public KeyboardWatcher(Activity activity) {
activityRef = new WeakReference<>(activity);
initialize();
}
/** * Sets listener. * * @param onKeyboardToggleListener the on keyboard toggle listener */ public void setListener(OnKeyboardToggleListener onKeyboardToggleListener) {
onKeyboardToggleListenerRef = new WeakReference<>(onKeyboardToggleListener);
}
/** * Destroy. */ public void destroy() {
if (rootViewRef.get() != null)
if (Build.VERSION.SDK_INT >= 16) {
rootViewRef.get().getViewTreeObserver().removeOnGlobalLayoutListener(viewTreeObserverListener);
} else {
rootViewRef.get().getViewTreeObserver().removeGlobalOnLayoutListener(viewTreeObserverListener);
}
}
private void initialize() {
if (hasAdjustResizeInputMode()) {
viewTreeObserverListener = new GlobalLayoutListener();
rootViewRef = new WeakReference<>(activityRef.get().findViewById(Window.ID_ANDROID_CONTENT));
rootViewRef.get().getViewTreeObserver().addOnGlobalLayoutListener(viewTreeObserverListener);
} else {
throw new IllegalArgumentException(String.format("Activity %s should have windowSoftInputMode=\"adjustResize\"" +
"to make KeyboardWatcher working. You can set it in AndroidManifest.xml", activityRef.get().getClass().getSimpleName()));
}
}
private boolean hasAdjustResizeInputMode() {
return (activityRef.get().getWindow().getAttributes().softInputMode & WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) != 0;
}
/** * The interface On keyboard toggle listener. */ public interface OnKeyboardToggleListener {
/** * On keyboard shown. */ void onKeyboardShown();
/** * On keyboard closed. */ void onKeyboardClosed();
}
private class GlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
/** * The Initial value. */ int initialValue;
/** * The Has sent initial action. */ boolean hasSentInitialAction;
/** * The Is keyboard shown. */ boolean isKeyboardShown;
@Override public void onGlobalLayout() {
if (initialValue == 0) {
initialValue = rootViewRef.get().getHeight();
} else {
if (initialValue > rootViewRef.get().getHeight()) {
if (onKeyboardToggleListenerRef.get() != null) {
if (!hasSentInitialAction || !isKeyboardShown) {
isKeyboardShown = true;
onKeyboardToggleListenerRef.get().onKeyboardShown();
}
}
} else {
if (!hasSentInitialAction || isKeyboardShown) {
isKeyboardShown = false;
rootViewRef.get().post(new Runnable() {
@Override public void run() {
if (onKeyboardToggleListenerRef.get() != null) {
onKeyboardToggleListenerRef.get().onKeyboardClosed();
}
}
});
}
}
hasSentInitialAction = true;
}
}
}
}
Implement it and register for it in oncreate
keyboardWatcher = new KeyboardWatcher(this);
keyboardWatcher.setListener(this);
and then override following mehods
@Overridepublic void onKeyboardShown() {
}
@Overridepublic void onKeyboardClosed() {
}
No comments:
Post a Comment