View Binding in Android | How to use View Binding in Android | Kotlin | Android | View Binding
Hello Guys, This tutorial will help you understand the concept of view binding in Android using Kotlin.
Short Description:- View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.
In most cases, view binding replaces #findViewById.
Enabling the ViewBinding Feature: There is a need to enabling the ViewBinding feature in Android Studio 4.0 and above, inside the app-level build gradle file.
STEP 1 : Create new Android Studio Project.
STEP 2 : After Creating new Project Setup build.gradle and sync project
android {
buildFeatures {
viewBinding = true
}
}
STEP 3: Here’s the activity_main.xml layout
<?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/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Title"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@id/ivImage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/ivImage"
android:layout_width="250dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="250dp"/>
<Button
android:id="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Click"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ivImage" />
</androidx.constraintlayout.widget.ConstraintLayout>
STEP 4: Here’s the MainActivity.kt Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import co.ls.androidviewbinding.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
//creating binding class for activity_main.xml
//which is generated automatically.
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var binding: ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// call a button click directly using binding
binding.btnClick.setOnClickListener {
// set image when button click
binding.ivImage.setImageResource(R.drawable.kotlin)
//set text wehn button click
binding.tvTitle.setText("View Binding Features in Android")
}
}
}
// All code is done let's run the app
STEP 5 : And finally run the App and See the output
Show More Tutorials And Feel free to SUBSCRIBE to my YOUTUBE channel Thank You :-)