공식문서 보러가기

[android:windowSoftInputMode](<https://developer.android.com/guide/topics/manifest/activity-element.html>)

액티비티의 기본 창이 터치형 소프트 키보드를 포함하는 창과 상호작용하는 방법을 나타냅니다. 이 속성 설정은 다음 두 가지에 영향을 미칩니다.

설정은 다음 표에 나열된 값 중 하나이거나 'state...' 값 하나와 'adjust...' 값 하나를 조합한 것이어야 합니다. 한 그룹에 여러 값(예: 여러 'state...' 값)을 설정하면 정의되지 않은 결과가 발생합니다. 개별 값은 세로 막대(|)로 구분됩니다.

키보드 Visibility

액티비티 진입 시 바로 edittext focus & keyboard show up

<activity
            android:name=".keyboard.KeyboardMainActivity"
            android:exported="true"
            android:windowSoftInputMode="stateVisible"> 👈
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
class KeyboardMainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityKeyboardBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityKeyboardBinding.inflate(layoutInflater)
        setContentView(binding.root) 

        binding.editText.requestFocus() 👈
    }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="<http://schemas.android.com/apk/res/android>"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:padding="10dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:background="@drawable/ic_launcher_background"
        android:src="@drawable/ic_launcher_foreground" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button_hide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hide" />

</androidx.appcompat.widget.LinearLayoutCompat>

editText에 requestFoucs를 준다.
manifest에서는 android:windowSoftInputMode="stateVisible"로 설정한다.

함수를 호출 했을 때 edittext focus & keyboard show up

binding.buttonShow.setOnClickListener {
            showSoftKeyboard(binding.editText)
        }

fun showSoftKeyboard(view: View) {
        if (view.requestFocus()) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
        }
    }

//mainfest는 따로 지정 x

함수를 호출 했을 때 edittext unFocus& keyboard show off

binding.buttonHide.setOnClickListener {
            hideSoftKeyboard(this,binding.editText.windowToken)
            binding.editText.clearFocus()
        }

fun hideSoftKeyboard(context: Context, windowToken: IBinder) {
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(windowToken, 0)
    }

//mainfest는 따로 지정 x

키보드에 따른 화면 사이즈 조절

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

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
    xmlns:app="<http://schemas.android.com/apk/res-auto>"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <View
        android:id="@+id/space"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@color/teal_200"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button_hide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hide"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/space" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button_hide" />

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purple_200"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

아무설정 x - 기본값 adjustUnspecified 이 설정된다.

<activity
            android:name=".keyboard.KeyboardMainActivity"
            android:exported="true"
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Screenshot_20230113_152836_My Application.jpg

editText를 가리지 않게 화면이 전체적으로 키보드 위로 올라감.

adjustPan - 키보드가 현재 포커스를 가리지 않고 사용자가 입력하는 것을 항상 볼 수 있도록 창의 콘텐츠가 자동으로 이동

<activity
            android:name=".keyboard.KeyboardMainActivity"
            android:exported="true"
            android:windowSoftInputMode="stateUnspecified|adjustPan"> 👈
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Screenshot_20230113_152943_My Application.jpg