프로그래밍 언어/Java

[Android] 키보드 팝업 상태 리스너 구현하기 (The way to check state of keyboard pop up )

happy_life 2022. 2. 12. 17:19

키보드 팝업 상태 리스너 구현하기 

Android 에서 실제로 키보드 팝업 상태 리스너를 제공하지 않으므로 직접 구현해야한다.

 

키보드가 올라왔을 때와 내려왔을 때, 키보드와 전체 화면 크기 차이의 비교를 통해 키보드 팝업 상태를 체크하고

이에 대해 메소드를 override해서 구현하는 코드이다.

The way to check the state of keyboard pop up 

 

1. android:windowsoftInputMode를 adjustResize로 설정한다. 

android:windowSoftInputMode="adjustResize"

 

 

2.SoftKeyboardDectectorView 클래스를 만든다.

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class SoftKeyboardDectectorView extends View {

    private boolean mShownKeyboard;
    private OnShownKeyboardListener mOnShownSoftKeyboard;
    private OnHiddenKeyboardListener onHiddenSoftKeyboard;

    public SoftKeyboardDectectorView(Context context) {
        this(context, null);
    }

    public SoftKeyboardDectectorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Activity activity = (Activity)getContext();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
        int diffHeight = (screenHeight - statusBarHeight) - h;
        if (diffHeight > 100 && !mShownKeyboard) { // 모든 키보드는 100px보다 크다고 가정
            mShownKeyboard = true;
            onShownSoftKeyboard();
        } else if (diffHeight < 100 && mShownKeyboard) {
            mShownKeyboard = false;
            onHiddenSoftKeyboard();
        }
        super.onSizeChanged(w, h, oldw, oldh);
    }

    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 {
        public void onShowSoftKeyboard();
    }

    public interface OnHiddenKeyboardListener {
        public void onHiddenSoftKeyboard();
    }
}

 

 

3.사용하고자 하는 activity에서 다음과 같이 코드를 작성한다.

 

final SoftKeyboardDectectorView softKeyboardDectector = new SoftKeyboardDectectorView(this);
        addContentView(softKeyboardDectector,new FrameLayout.LayoutParams(-1,-1));

        //키보드 상태 리스너 메소드 오버라이드
        //키보드 팝업 때
        softKeyboardDectector.setOnShownKeyboard(new SoftKeyboardDectectorView.OnShownKeyboardListener() {
            @Override
            public void onShowSoftKeyboard() {
                Log.d(TAG,"키보드 올라와요");
               
               //키보드 팝업 시 이벤트 쓰는 부분 
            }
        });

        //키보드 사라질 때
        softKeyboardDectector.setOnHiddenKeyboard(new SoftKeyboardDectectorView.OnHiddenKeyboardListener() {
            @Override
            public void onHiddenSoftKeyboard() {
                Log.d(TAG,"키보드 내려가요");
                includeBottom.setVisibility(View.VISIBLE);
                
                //키보드 해제 시 이벤트 쓰는 부분
            }
        });

 

 

 

로그를 남겨 두었으니, 이 로그를 보고 키보드 상태를 직접 체크해보고 사용하길 바랍니다.