๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android Kotlin] ์•ˆ๋“œ๋กœ์ด๋“œ ์ฝ”ํ‹€๋ฆฐ - ๋ทฐ ๋ฐ”์ธ๋”ฉ (View Binding) ๋ณธ๋ฌธ

๐Ÿค–Android/Kotlin

[Android Kotlin] ์•ˆ๋“œ๋กœ์ด๋“œ ์ฝ”ํ‹€๋ฆฐ - ๋ทฐ ๋ฐ”์ธ๋”ฉ (View Binding)

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 10. 11. 18:58
๋ฐ˜์‘ํ˜•

 

์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ ์‹œ xml ์—ฐ๊ฒฐํ•˜๋Š” findViewById() ์ „ํ†ต์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ–ˆ์—ˆ๋Š”๋ฐ ์ฝ”ํ‹€๋ฆฐ์„ ๋ฐฐ์šฐ๋ฉด์„œ ์ฝ”ํ‹€๋ฆฐ์—์„œ๋งŒ ์ œ๊ณตํ•˜๋Š” ๋ทฐ ๋ฐ”์ธ๋”ฉ(View Binding)์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ชจ๋กœ findViewById() ๋ณด๋‹ค ์žฅ์ ์ด ๋งŽ์•„์„œ ๋ฐฐ์šฐ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด์ œ ๋ทฐ ๋ฐ”์ธ๋”ฉ์œผ๋กœ xml ํ™”๋ฉด์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

0. ๋ทฐ ๋ฐ”์ธ๋”ฉ(viewBinding)์˜ ์žฅ์ 

  • ํƒ€์ž… ์•ˆ์ •์„ฑ : XML์—์„œ ์ •์˜๋œ ๋ทฐ๋ฅผ ์ž˜๋ชป ์ฐธ์กฐํ•  ์œ„ํ—˜์ด ์ค„์–ด๋“ ๋‹ค.
  • ์ปดํŒŒ์ผ ํƒ€์ž„ ์ฒดํฌ : ์˜ค๋ฅ˜๋ฅผ ์ปดํŒŒ์ผ ํƒ€์ž„์— ์žก์„ ์ˆ˜ ์žˆ์–ด์„œ ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ์ค„์–ด๋“ ๋‹ค.
  • ์ฝ”๋“œ ๊ฐ„์†Œํ™” : findViewById()๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๊ณ , ์ค‘๋ณต ์ฝ”๋“œ๊ฐ€ ์ค„์–ด๋“ ๋‹ค.
  • null ์•ˆ์ „์„ฑ : Kotlin์—์„œ๋Š” null ์•ˆ์ „์„ฑ์„ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ๋” ์•ˆ์ „ํ•ด์ง„๋‹ค.

 

1. viewBinding ์„ค์ •

build.gradle(Module :app) ํŒŒ์ผ์— viewBinding ์„ค์ •์„ ์ถ”๊ฐ€

[Sync Now] ํด๋ฆญํ•ด์„œ ์„ค์ • ์ ์šฉ

android {
    ...

    buildFeatures {
        viewBinding = true
    }
}

 

2.activity_main.xml ํŒŒ์ผ ์ž‘์„ฑ

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textSay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnSay"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textSay" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

3. ๋ ˆ์ด์•„์›ƒ ํŒŒ์ผ ๋ฐ”์ธ๋”ฉ์œผ๋กœ ์ƒ์„ฑ

viewBinding์ด ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ๋“œ๋กœ์ด๋“œ๊ฐ€ ๋ ˆ์ด์•„์›ƒ ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ๋ฐ”์ธ๋”ฉ์œผ๋กœ ์ƒ์„ฑ

๊ทธ ๋‹ค์Œ ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๋ฐ”์ธ๋”ฉ์˜ root ๋ทฐ๋ฅผ setContentView์— ์ „๋‹ฌํ•œ๋‹ค.

package kr.co.hanbit.sayhello

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kr.co.hanbit.sayhello.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // inflate(layoutInflater) ๋ชจ๋“  Activity์—์„œ ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
        val binding = ActivityMainBinding.inflate(layoutInflater)
		
        // bindung.root๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ™”๋ฉด ์•ˆ์˜ ๋ฒ„ํŠผ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
        setContentView(binding.root)

    }
}

 

4. ๋ทฐ์˜ id ์ ‘๊ทผํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ

๋ฒ„ํŠผ id์— ๋ฆฌ์Šคํ„ฐ(Listner)๋ฅผ ์„ค์ •ํ•œ๋‹ค.
(๋ฆฌ์Šค๋„ˆ์˜ ์—ญํ• ์€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ๋‚ด๋ถ€์˜ ์ฝ”๋“œ๋ฅผ ๋™์ž‘์‹œํ‚ค๋Š” ๊ฒƒ)

package kr.co.hanbit.sayhello

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kr.co.hanbit.sayhello.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        val binding = ActivityMainBinding.inflate(layoutInflater)
		
        setContentView(binding.root)
        
        // id๊ฐ€ btnSay์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด 
        // id๊ฐ€ textSay์ธ textView๊ฐ€ "Hello Kotlin!!"์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.
        binding.btnSay.setOnClickListener {
            binding.textSay.text = "Hello Kotlin!!"
        }

    }
}
๋ฐ˜์‘ํ˜•