Last active
February 28, 2017 09:01
-
-
Save extralam/ff3a6357522a866ea49ec79463ff22f4 to your computer and use it in GitHub Desktop.
Android Keyboard Open or Close helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.graphics.Rect; | |
import android.os.Build; | |
import android.util.DisplayMetrics; | |
import android.view.View; | |
import android.view.ViewTreeObserver; | |
import android.view.WindowManager; | |
import static android.os.Build.VERSION.SDK_INT; | |
import static android.os.Build.VERSION_CODES.JELLY_BEAN; | |
public class Keyboardhelper { | |
private final int MIN_KEYBOARD_HEIGHT = 100; | |
private int keyboardHeight = 0; | |
private KeyBoardListener mKeyBoardListener; | |
private View refView; | |
private Context mContext; | |
public Keyboardhelper(View rootView , KeyBoardListener l){ | |
this.refView = rootView; | |
this.mContext = rootView.getContext(); | |
this.mKeyBoardListener = l; | |
this.refView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener); | |
} | |
public void dismiss(){ | |
AppLog.d("dismiss removeOnGlobalLayoutListener " + refView + " ," + onGlobalLayoutListener); | |
if(refView != null && onGlobalLayoutListener != null) { | |
removeOnGlobalLayoutListener(refView, onGlobalLayoutListener); | |
} | |
} | |
public interface KeyBoardListener{ | |
void onClosed(); | |
void onOpened(); | |
} | |
@TargetApi(JELLY_BEAN) | |
void removeOnGlobalLayoutListener(final View v, final ViewTreeObserver.OnGlobalLayoutListener listener) { | |
if (SDK_INT < JELLY_BEAN) { | |
//noinspection deprecation | |
v.getViewTreeObserver().removeGlobalOnLayoutListener(listener); | |
} else { | |
v.getViewTreeObserver().removeOnGlobalLayoutListener(listener); | |
} | |
} | |
int getUsableScreenHeight() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
final DisplayMetrics metrics = new DisplayMetrics(); | |
final WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); | |
windowManager.getDefaultDisplay().getMetrics(metrics); | |
return metrics.heightPixels; | |
} else { | |
return refView.getRootView().getHeight(); | |
} | |
} | |
private final ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = | |
new ViewTreeObserver.OnGlobalLayoutListener() { | |
private final Rect rect = new Rect(); | |
private boolean isKeyboardOpen; | |
@Override public void onGlobalLayout() { | |
refView.getWindowVisibleDisplayFrame(rect); | |
int keyboardHeight = getUsableScreenHeight() - (rect.bottom - rect.top); | |
boolean isOpen = keyboardHeight > MIN_KEYBOARD_HEIGHT; | |
if (isOpen == isKeyboardOpen) { | |
return; | |
} | |
isKeyboardOpen = isOpen; | |
if(mKeyBoardListener != null){ | |
if(isKeyboardOpen) | |
mKeyBoardListener.onOpened(); | |
else | |
mKeyBoardListener.onClosed(); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment