코딩스토리

[Android/안드로이드] 키보드로 액티비티 화면 조정 adjustPan 본문

Android/유용한 기술

[Android/안드로이드] 키보드로 액티비티 화면 조정 adjustPan

라크라꾸 2020. 9. 10. 01:51

키보드를 올렸을 때 안에 있는 레이아웃이 전체적으로 움직이거나 고정시키고 싶을 때가 있습니다.

이번 포스팅에서는 키보드의 영향에 따라 레이아웃을 조정하는 코드를 작성해보겠습니다.

 

우선 키보드 화면조정의 속성에는 다음과 같습니다.

 

  • Default(설정이 안 된 경우) : ajdustUnspecified와 stateUnspecified 적용됩니다.
  • adjustPan : 키보드가 올라올 때  UI화면도 같이 위로 올라갑니다.
  • adjustResize : 키보드가 올라갈 때 액티비티의 크기를 조정해줍니다.
  • adjustUnspecified : 시스템이 알아서 상황에 맞는 옵션을 설정해줍니다.
  • stateHidden : 액티비티를 실행했을 때 키보드가 자동으로 올라오는것을 방지합니다.
  • stateVisible : 액티비티를 실행하면 키보드가 자동으로 올라옵니다.
  • stateUnspecified : 시스템이 적절한 키보드 상태를 설정해줍니다.

 

위는 adjustPan을 적용한 것으로, 키보드가 올라갈 때 UI화면도 같이 올라가게 되는것을 확인할 수 있습니다.

manifests.xml

<activity android:name=".MainActivity"
            android:windowSoftInputMode="adjustPan">

 

위는 adjustResize로 키보드가 올라갈 때 액티비티는 고정되어있습니다. 하지만 너무 고정되어있는 탓에 입력할 수 있는 EditText가 보이지 않아 문제가 있습니다. 

manifest.xml

<activity android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize">

이 문제를 해결하기 위해서는 xml코드에서 레이아웃을 바꿔줘야합니다.

 

 

저는 최상위 레이아웃을 RelativeLayout로 만들어 준 뒤, EditText를 바닥에 붙여주니 다음과 같이 키보드 위에 EditText가 붙는 것을 확인할 수 있습니다.

 


SampleCode

RelativeLayout의 구성은 다은과 같습니다.

activity_main.xml

	
    <?xml version="1.0" encoding="utf-8"?>
	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:app="http://schemas.android.com/apk/res-auto"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    tools:context=".MainActivity">
	
	    <LinearLayout
	        android:orientation="vertical"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content">
	
	        <Button
	            android:layout_marginTop="5dp"
	            android:id="@+id/btn_hide_keyboard"
	            android:text="키보드 숨기기"
	            android:layout_width="match_parent"
	            android:layout_height="wrap_content"/>
	
	        <Button
	            android:layout_marginTop="5dp"
	            android:id="@+id/btn_show_keyboard"
	            android:text="키보드 보이기"
	            android:layout_width="match_parent"
	            android:layout_height="wrap_content"/>
	
	        <View
	            android:layout_marginTop="5dp"
	            android:background="#AB98DC"
	            android:layout_width="match_parent"
	            android:layout_height="200dp"/>
	
	        <View
	            android:layout_marginTop="5dp"
	            android:background="#E4D972"
	            android:layout_width="match_parent"
	            android:layout_height="200dp"/>
	
	        <View
	            android:layout_marginTop="5dp"
	            android:background="#2A7495"
	            android:layout_width="match_parent"
	            android:layout_height="150dp"/>
	    </LinearLayout>
	
	
	    <EditText
	        android:id="@+id/editText"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:layout_alignParentBottom="true"
	        app:layout_constraintBottom_toBottomOf="parent" />
	</RelativeLayout>
	

EditText만 바닥에 붙여주기만 하면 키보드 위에 붙도록 구현할 수 있습니다.

Comments