Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
Data Binding in Android with Example
Next article icon

Data Binding in Android with Example

Last Updated : 18 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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).

gradle_module_java


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_Java


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:

databinding


Next Article
Data Binding in Android with Example

J

jangirkaran17
Improve
Article Tags :
  • Java
  • Android
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Kotlin Android
  • Java-Android
Practice Tags :
  • Java

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences