SoftKeyboard의 크기를 알아내는 뷰를 새로 작성하여 사용함
View를 상속 받아 사용하며, View의 onSizeChanged 함수를 활용하여 뷰 변화를 통해 키보드의 높이를 알아낼 수 있다.
SoftKeyboardDectectorView.java
package com.wescan.alo.view;
/**
* Created by hazuki21 on 16. 3. 10..
*/
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.View;
public class SoftKeyboardDectectorView extends View {
private Context context;
private boolean mShownKeyboard;
private OnShownKeyboardListener mOnShownSoftKeyboard;
private OnHiddenKeyboardListener onHiddenSoftKeyboard;
private int diffHeight;
public SoftKeyboardDectectorView(Context context) {
this(context, null);
this.context = context;
}
public SoftKeyboardDectectorView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SoftKeyboardDectectorView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Activity activity = (Activity) context;
Rect windowRect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(windowRect);
Rect screenRect = new Rect();
activity.getWindowManager().getDefaultDisplay().getRectSize(screenRect);
diffHeight = screenRect.bottom - windowRect.bottom;
Log.i("Main", "TestAPI diffHeight : " + diffHeight);
if (diffHeight > 0 && !mShownKeyboard) { // 모든 키보드는 0px보다 크다고 가정
mShownKeyboard = true;
onShownSoftKeyboard();
} else if (diffHeight <= 0 && mShownKeyboard) {
mShownKeyboard = false;
onHiddenSoftKeyboard();
}
super.onSizeChanged(w, h, oldw, oldh);
}
public boolean getKeyboardState() {
return mShownKeyboard;
}
public int getKeyboardHeight() {
return this.diffHeight;
}
public void onHiddenSoftKeyboard() {
if (onHiddenSoftKeyboard != null)
onHiddenSoftKeyboard.onHiddenSoftKeyboard();
}
public void onShownSoftKeyboard() {
if (mOnShownSoftKeyboard != null)
mOnShownSoftKeyboard.onShowSoftKeyboard();
}
public void setOnShownKeyboard(OnShownKeyboardListener listener) {
mOnShownSoftKeyboard = listener;
}
public void setOnHiddenKeyboard(OnHiddenKeyboardListener listener) {
onHiddenSoftKeyboard = listener;
}
public interface OnShownKeyboardListener {
void onShowSoftKeyboard();
}
public interface OnHiddenKeyboardListener {
void onHiddenSoftKeyboard();
}
}
Activity의 onCreate 부분에서 사용하면 된다. 예시일 뿐이니 참고하여 사용해야 한다.
final SoftKeyboardDectectorView softKeyboardDecector = new SoftKeyboardDectectorView(this);
addContentView(softKeyboardDecector, new FrameLayout.LayoutParams(-1, -1));
softKeyboardDecector.setOnShownKeyboard(new OnShownKeyboardListener() {
@Override
public void onShowSoftKeyboard() {
//키보드 등장할 때
}
});
softKeyboardDecector.setOnHiddenKeyboard(new OnHiddenKeyboardListener() {
@Override
public void onHiddenSoftKeyboard() {
// 키보드 사라질 때
}
})