Data Binding in Android with Example
Last Updated :
23 Jul, 2025
In Android, the 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.
Step by Step Implementation:
Step 1: Create a New Project
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2: Enable Data Binding
Navigate to Gradle Scripts > build.gradle.kts(module level).
Add the below code snippet to the build.gradle.kts (module level) file under the android{} scope to activate Data Binding in the application:
buildFeatures {
dataBinding = true
}
Step 3: Create a model class
Navigate app > kotlin+java > {package-name}. Right click on it and go to New > Kotlin Class/File or Java Class. Set the name Company for the file. Add the following code to the file.
Company.java
package org.geeksforgeeks.demo;
public class Company {
private String name;
private String website;
public Company(String name, String website) {
this.name = name;
this.website = website;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
}
Company.kt
package org.geeksforgeeks.demo
data class Company (
val name: String,
val website: String
)
Step 4: Working on activity_main.xml
Navigate to the app > res > layout > activity_main.xml and add the below code to that file.
activity_main.xml:
activity_main.xml
<layout
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">
<data>
<variable
name="company"
type="org.geeksforgeeks.demo.Company" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="@{company.name}"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/website"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{company.website}"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Step 5: Working on Main Activity file
Navigate to the MainActivity.java/MainActivity.kt file and use the following code in it. Comments are added to the code to have a better understanding.
MainActivity.java
package org.geeksforgeeks.demo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import org.geeksforgeeks.demo.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setting up Data Binding
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
// Creating Company object and binding it to the layout
Company company = new Company("GeeksforGeeks", "www.geeksforgeeks.org");
binding.setCompany(company);
}
}
MainActivity.kt
package org.geeksforgeeks.demo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import org.geeksforgeeks.demo.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val company = Company("GeeksforGeeks", "www.geeksforgeeks.org")
binding.company = company
}
}
Output:
Data Binding in Android with Example