Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 26 |
27 | 28 | 29 | 30 |
Tags
- 안드로이드 스튜디오 인터넷 연결 안되어 있을 때
- 안드로이드 스튜디오 커스텀 다이얼로그
- savedinstancestate
- 아파치란
- 안드로이드 스튜디오 반복되는 레이아웃 코드
- 객체지향 프로그래밍 5가지 원칙
- 변수
- 안드로이드 디자인패턴
- 이중for문 사용 안하기
- Kotlin
- java
- Thread
- 다른 객체 리스트의 비교
- 디자인 패턴 예제
- 안드로이드 스튜디오 custom dialog
- dagger error
- apache nginx
- 안드로이드 스튜디오 인터넷 연결 확인
- edittext 연결
- AAC
- apache란
- java thread 예제
- 안드로이드 스튜디오 tts
- recyclerview item recycle
- LifeCycle
- 안드로이드 스튜디오 style
- 자바 스레드 예제
- hilt error
- 리사이클러뷰 아이템 재사용
- 아파치 엔진엑스
Archives
- Today
- Total
Sam Story
뷰 바인딩 (ViewBinding) 본문
1. 뷰바인딩 (ViewBinding) 이란 ?
레이아웃 XML 파일에 대한 액티비티 클래스를 정의하지 않고 자동적으로 뷰에 대해 연결을 생성해 준다.
자동적으로 뷰에 대한 연결을 생성하니 findViewById를 안쓰는 장점이 있고
유효하지 않은 뷰 ID 로 인해 생기는 Null Pointer Exception의 발생 위험이 없다.
2. 예제
오늘의 예제는 아주 간단한 예제로
버튼을 눌렀을때 텍스트 뷰의 텍스트가 바뀌는 예제다.
기존의 코드의 경우는 뷰를 선언하고 초기화 해주는것을
액티비티 코드에서 처리를 해주어야 했지만
뷰바인딩을 할 경우 binding 값을 선언과 초기화 해주고
그 binding 값으로 뷰들의 속성,값을 초기화 해줄 수 있다.
먼저 viewBinding을 build.gradle에 추가해준다.
android {
buildFeatures {
viewBinding = true
}
}
레이아웃
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="30sp"
android:text="뷰 바인딩 테스트"
/>
<Button
android:id="@+id/button"
android:layout_margin="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼"/>
</LinearLayout>
액티비티
package com.example.viewbinding_aac
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.viewbinding_aac.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
// binding 객체를 선언 해준다.
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 바인딩 객체를 촉화 해준 후 setContentView 의 매개변수로 binding.root을 지정해준다.
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// 버튼의 클릭리스너
binding.button.setOnClickListener {
// 텍스트 뷰의 값을 바꿔준다
binding.textView.text = "버튼을 눌렀습니다"
}
}
}
필요한 내용은 주석으로 적어놨다.
실행 결과
이러한 ViewBinding을 쓰게 되면 코드도 굉장히 간소화 되면서
일일히 findViewById로 뷰를 선언하고 초기화 해주지 않아도 된다.
MVC 패턴으로 해커톤 프로젝트를 진행 할 때 굉장히 편리하다고 느낀 기능이였다.
다음번에는 MVC 패턴에 대한 포스팅을 해봐야겠다.
'Android' 카테고리의 다른 글
데이터 바인딩 (Data Binding) (1) | 2024.04.01 |
---|---|
레트로핏 (Retrofit) (0) | 2024.03.26 |
Visibility 속성 (2) | 2024.03.16 |
쉐어드프리퍼런스(Sharedpreferences) (0) | 2024.03.12 |
리사이클러뷰(RecyclerView) (1) | 2024.03.12 |