๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’š ๊ฐœ๋ฐœ/์•ˆ๋“œ๋กœ์ด๋“œ

[์•ˆ๋“œ๋กœ์ด๋“œ] findViewById ๊ทธ๋ฆฌ๊ณ  View Binding

by devshin.kr 2021. 7. 28.
728x90

- ๊ณตํ†ต 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

- ๊ธฐ๋ณธ MainActivity.java ์ฝ”๋“œ

package com.devshin93.apktest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

 

 

- findViewById ์ ์šฉํ•œ MainActivity.java ์ฝ”๋“œ

package com.devshin93.apktest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.textView);
        textView.setText("findViewById");
    }
}

 

 

- View Binding ์— ํ•„์š”ํ•œ configuration ์ถ”๊ฐ€ build.gradle (:app) 

Databinding ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Android Gradle ํ”Œ๋Ÿฌ๊ทธ์ธ๊ณผ ํ•จ๊ป˜ ๋ฒˆ๋“ค๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ข…์† ํ•ญ๋ชฉ์„ ์„ ์–ธํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ, ์‚ฌ์šฉ ์„ค์ •์€ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

android {
	...
    
    // Android Studio 3.6 ๋ถ€ํ„ฐ 4.0 ์ด์ „๊นŒ์ง€
    viewBinding {
    	enabled = true
    }
    
    // Android Studio 4.0 ๋ถ€ํ„ฐ
    buildFeatures {
    	viewBinding true
    }
}

 

- View Binding ์ ์šฉํ•œ MainActivity.java ์ฝ”๋“œ

package com.devshin93.apktest;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.devshin93.apktest.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        binding.textView.setText("View Binding");
    }
}

 

 

์ฐธ์กฐ : https://developer.android.com/topic/libraries/view-binding

 

๋ทฐ ๊ฒฐํ•ฉ  |  Android ๊ฐœ๋ฐœ์ž  |  Android Developers

๋ทฐ ๊ฒฐํ•ฉ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ทฐ์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉ ์„ค์ •๋œ ๋ทฐ ๊ฒฐํ•ฉ์€ ๋ชจ๋“ˆ์— ์žˆ๋Š” ๊ฐ XML ๋ ˆ์ด์•„์›ƒ ํŒŒ์ผ์˜ ๊ฒฐํ•ฉ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”์ธ๋”ฉ ํด๋ž˜์Šค์˜

developer.android.com

 

์ฐธ์กฐ : https://developer.android.com/jetpack/androidx/releases/databinding?hl=ko 

 

Databinding  |  Android ๊ฐœ๋ฐœ์ž  |  Android Developers

Databinding ๋ ˆ์ด์•„์›ƒ์˜ UI ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์„ ์–ธ์  ํ˜•์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์•ฑ์˜ ๋ฐ์ดํ„ฐ ์†Œ์Šค์— ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ตœ๊ทผ ์—…๋ฐ์ดํŠธ ํ˜„์žฌ ์•ˆ์ •ํ™” ๋ฒ„์ „ ๋‹ค์Œ ๋ฒ„์ „ ํ›„๋ณด ๋ฒ ํƒ€ ๋ฒ„์ „ ์•ŒํŒŒ ๋ฆด๋ฆฌ์Šค 2019๋…„ 9์›” 5์ผ 3.5.0 - - 3.6.0-al

developer.android.com

 

๋Œ“๊ธ€