Android EditText 키보드 위로 - Android EditText kibodeu wilo


1. [Manifest -> 해당 Activity]

2. android:windowSoftInputMode="adjustUnspecified|adjustPan" 추

        <activity android:name=".menu_club.post.BoardShowActivity"
                android:theme="@style/AppTheme.NoActionBar"
                android:windowSoftInputMode="adjustUnspecified|adjustPan" />

Reference

https://musesong.tistory.com/entry/안드로이드-소프트키보드에-가려진-EditText-키보드-위로-올리기

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

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

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

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

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

manifests.xml

<activity android:name=".MainActivity"
            android:windowSoftInputMode="adjustPan">
Android EditText 키보드 위로 - Android EditText kibodeu wilo

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

manifest.xml

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

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

Android EditText 키보드 위로 - Android EditText kibodeu wilo

저는 최상위 레이아웃을 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만 바닥에 붙여주기만 하면 키보드 위에 붙도록 구현할 수 있습니다.


카테고리 없음

2018. 1. 9. 21:08

카카오톡(KakaoTalk)에서 글을 쓰는 구간인 EditText에 Focus가 활성화되어있으면 이렇게 EditText레이아웃이 키보드 위로 올라오게 된다.

키보드가 올라오자 입력값을 전달할 레이아웃도 같이 올라온 모습이다.

위같은 방법을 사용하기위해서는 아래의 코드를 보아야 한다.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#eee">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

</LinearLayout>

cs

위의 코드에서 하나 특이한 점이 있다.

레이아웃 안에 오직 LinearLayout만 1의 weight값을 준것이다.

저렇게 하면 밑에있는 EditText를 눌러 포커스를 지정했을때 키보드 위로 EditText가 얹혀지게 된다.

하지만 어딘가 불안정하게 얹히는것을 볼 수 있는데,

이걸 해결하기위해 매니페스트(manifest)의 해당 액티비티에 코드 하나만 집어넣으면 된다.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<?xml version="1.0" encoding="utf-8"?>

<manifest

xmlns:android="http://schemas.android.com/apk/res/android"

package="com.bino.ranpick">

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".Activity.Splash_Activity">

</activity>

<activity

android:name=".Activity.Main_Activity"

android:windowSoftInputMode="adjustResize">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

cs

해당 액티비티에 위의 17번 행처럼

    android:windowSoftInputMode="adjustResize"

라는 코드를 넣어주면

옵션 이름처럼 키보드 레이아웃에 맞춰서 Resizing해준다.