낭만 프로그래머

Android 개발시 로그인 화면에서 키보드가 버튼을 가리는 문제 해결 본문

Android

Android 개발시 로그인 화면에서 키보드가 버튼을 가리는 문제 해결

조영래 2020. 5. 15. 15:03

앱 개발시에 로그인 화면이 필요한데 에디터를 누를 시에 키보드가 올라오면서 로그인 버튼을 가리는 문제가 발생했다.
키보드 이벤트를 사용하여 해결해 보자

1. AndroidManifest.xml 파일에 android:windowSoftInputMode="adjustResize" 추가

...

<activity
	android:name=".LoginActivity"
    android:label="@string/title_activity_login"
    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden"
    android:windowSoftInputMode="adjustResize"
    android:theme="@style/AppTheme.NoActionBar">
	<intent-filter>
    	<action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
	</intent-filter>
</activity>
        
...

 

2. Layout 파일 작성
    - ScrollView를 최상위로 넣기
    - android:id="@+id/svRoot" 처럼 id 필요함
    - android:fillViewport="true" 는 옵션이지만 세로를 화면 전체 채우려면 필요

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/svRoot"
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFBF00"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:gravity="end"
            android:orientation="horizontal">

            <com.beardedhen.androidbootstrap.BootstrapButton
                android:id="@+id/btnInitialization"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/initialize"
                app:bootstrapBrand="danger"
                app:bootstrapSize="sm"
                app:buttonMode="regular"
                app:roundedCorners="true"
                app:showOutline="true" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:text="@string/app_name"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="30dp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/ivPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/password" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/inputLayouttfPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:layout_marginBottom="20dp"
                    android:layout_weight="1">

                    <EditText
                        android:id="@+id/tfPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="10"
                        android:hint="@string/masterkey_name"
                        android:inputType="textPassword"
                        android:textAlignment="center"
                        android:textColor="#e80b0f"
                        android:textSize="30dp" />
                </android.support.design.widget.TextInputLayout>

                <CheckBox
                    android:id="@+id/cbShow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/passwordShow"
                    android:textSize="20dp" />
            </LinearLayout>

            <com.beardedhen.androidbootstrap.BootstrapButton
                android:id="@+id/btnLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="10dp"
                android:text="@string/ok"
                app:bootstrapBrand="secondary"
                app:bootstrapSize="lg"
                app:buttonMode="regular"
                app:roundedCorners="true"
                app:showOutline="false" />
        </LinearLayout>
</ScrollView>

 

3. JAVA 파일 작성

public class LoginActivity extends AppCompatActivity {

    private boolean keyboardListenersAttached = false;
    private ViewGroup rootLayout;
    private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = rootLayout.getRootView().getHeight() - rootLayout.getHeight();
            int contentViewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();

            if(heightDiff <= contentViewTop){
                rootLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        ((ScrollView)rootLayout).fullScroll(ScrollView.FOCUS_UP);
                    }
                });
            }
            else {
                //int keyboardHeight = heightDiff - contentViewTop;

                rootLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        ((ScrollView)rootLayout).fullScroll(ScrollView.FOCUS_DOWN);
                    }
                });
         }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        ...

        if (!keyboardListenersAttached) {
            rootLayout = (ViewGroup) findViewById(R.id.svRoot);
            rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);

            keyboardListenersAttached = true;
        }

        ...
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (keyboardListenersAttached) {
            rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(keyboardLayoutListener);
        }
    }
	
	...

}