Synthetic Binding in Android
Last Updated :
16 Feb, 2023
In android application development, for every view in the XML, we need to get an instance of that view to get access to it for doing any operation on it, like changing any property of a view. This is a very common requirement in any android application development cycle. There are main two ways of android development.
- Data Binding: Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.
- View Binding: View Binding is one of the best features which provides the views to bind with the activity which is ongoing.
Other than these two bindings a way in Kotlin to do binding is called Synthetic Binding.
What is Synthetic Binding?
A feature of Kotlin, which automatically binds objects is there, we can just call it without any type of binding and can change any property of any view by just its ID. Like,
ViewId.property = change
Note: It replaces findViewById.
For showing Synthetic Binding, we are going to create an android application in android studio.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:gravity = "center"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< TextView
android:id = "@+id/textView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textSize = "24sp"
tools:text = "Hello World!" />
</ LinearLayout >
|
Step 3: Working with the Build.gradle (app) file
Add Kotlin extensions for synthetic binding. Inside plugins .
Plugins {
id 'kotlin-android-extensions'
}
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.example.viewbinding
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.setBackgroundColor(getColor(R.color.teal_200))
textSize=30f
ext= "GeeksForGeeks"
setTextColor(getColor(R.color.purple_500))
val aBar = supportActionBar
val cd = ColorDrawable(Color.parseColor( "#FF00FF00" ))
aBar?.setBackgroundDrawable(cd)
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...