Data Binding in Android with Example
Last Updated : 18 Feb, 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:
Similar Reads
Data Binding with ViewModel in Android DataBinding is one of the most important concepts to really improve the performance of your Android Application It plays a vital role in the maintenance of the many Android Architectures. In this article, we will learn about DataBinding in ViewModel In Android. Benefits of integrating Data binding i
5 min read
ArrayAdapter in Android with Example The Adapter acts as a bridge between the UI Component and the Data Source. It converts data from the data sources into view items that can be displayed into the UI Component. Data Source can be Arrays, HashMap, Database, etc. and UI Components can be ListView, GridView, Spinner, etc. ArrayAdapter is
3 min read
Android Sensors with Example In our childhood, we all have played many android games like Moto Racing and Temple run in which by tilting the phone the position of the character changes. So, all these happen because of the sensors present in your Android device. Most Android-powered devices have built-in sensors that measure mot
4 min read
Deep Linking in Android with Example Deep Linking is one of the most important features that is used by various apps to gather data inside their apps in the form of a URL link. So it becomes helpful for the users from other apps to easily share the data with different apps. In this article, we will take a look at the implementation of
7 min read
TreeView in Android with Example If you are looking for new UI designs to represent huge data, then there are so many ways to represent this type of data. You can use pie charts, graphs, and many more view types to implement these views. For displaying such huge data then we can prefer using a TreeView. TreeView is similar to that
4 min read