---
title: Android Data Binding - Part 1
description: Usually an android application is data driven, so Google released the Data Binding. A cleaner way to tie up the code logic to the view layer.
image: https://blog.avenuecode.com/hubfs/Images/Blog/Android%20Binding.jpg
---

[AvenueCode.com](https://avenuecode.com) [News](https://avenuecode.com/news) [Contact](https://avenuecode.com/contact)

[![Avenue Code Snippets Logo](https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/AC-Snippets---Black.png)](https://blog.avenuecode.com/?hsLang=en-us) *menu*

- Technology
  
  [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us) [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us) [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us) [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us) [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
  
  [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us) [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us) [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us) [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

- [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us)
- [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us)
- [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us)
- [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us)
- [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us)
- [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us)
- [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us)
- [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

# [Android Data Binding - Part 1](https://blog.avenuecode.com/android-data-binding-part-1-2)

- [Tweet](https://twitter.com/share)

*perm\_identity* [Marcus Vinicius Corrêa Barcelos](https://blog.avenuecode.com/android-data-binding-part-1-2/author/marcus-vinicius-corr%C3%AAa-barcelos?hsLang=en-us)

*schedule* 2/1/17 5:01 PM

> Android applications are, almost universally, data driven. All screens have a data object that can be fetched from the internet, database, or even created by the application user. Typically on Android apps, developers need to inflate the view, locate each view element using findById, and then set the appropriate value. To make this process faster and more effective, Google released the Data Binding Support Library, which provides a cleaner way to tie upthe code logic to the view layer, thereby minimizing the necessary work to reference each view element.

## **How it Works**

The Data Binding API uses the Java Annotation Processor to generate the java classes on compile time. It's a kind of java compile plugin that inspects the code, looking for particular annotations to generate or modify the code.

The only two requirements to use data binding are:

1. Android plugin for Gradle 1.5.0-alpha1 and
2. DataBinding enabled on your build.gradle file:

`android { `

`    ....`

`    dataBinding {`

`        enabled = true`

`    }`

`}`

All layouts should have the tag `<layout>:`

`<?xml version="1.0" encoding="utf-8"?> `

`<layout xmlns:android="http://schemas.android.com/apk/res/android" `

`    xmlns:tools="http://schemas.android.com/tools">`

 

`    ....`

 

`</layout> `

When the project is compiling, the plugin will look for layout files with this tag and generate an equivalent java version. This class will have the same name as the xml file, converting it to Pascal case and "Binding" suffix, so if the layout's name is *activity\_main.xml*, the java class will be *ActivityMainBinding.java*.

The layout inflate will be a little bit different, using the **DataBindingUtils.java** helper:

Inside activities:

`@Override`

`protected void onCreate(Bundle savedInstanceState) { `

`    super.onCreate(savedInstanceState);`

`    ActivityMainBinding binding =  DataBindingUtil.setContentView(this, R.layout.activity_main);`

`}`

Inside fragments:

`@Override`

`public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { `

`    ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);`

`    return binding.getRoot();`

`}`

## **Good Bye ButterKnife**

One of the best features provided by data binding is the layout binding. It is similar to [ButterKnife](http://jakewharton.github.io/butterknife/), but you don't need to create an instance of each view with the respective annotations. The entire process is automatic, and the plugin will generate a class with all elements. The only requirement is to define a view id.

*activity\_main.xml*

`<?xml version="1.0" encoding="utf-8"?> `

`<layout xmlns:android="http://schemas.android.com/apk/res/android" `

`    xmlns:tools="http://schemas.android.com/tools">`

 

`    <RelativeLayout`

`        android:id="@+id/activity_main"`

`        android:layout_width="match_parent"`

`        android:layout_height="match_parent"`

`        android:paddingBottom="@dimen/activity_vertical_margin"`

`        android:paddingLeft="@dimen/activity_horizontal_margin"`

`        android:paddingRight="@dimen/activity_horizontal_margin"`

`        android:paddingTop="@dimen/activity_vertical_margin"`

`        tools:context="com.binding.data.avenuecode.databindingexample.MainActivity">`

 

`        <TextView`

          ` ``android:id="@+id/hello_world_text_view"`

`            android:layout_width="wrap_content"`

`            android:layout_height="wrap_content"`

`            android:text="Hello World!" />`

`    </RelativeLayout>`

`</layout> `

The API generates the view instance inside the *ActivityMainBinding.java:*

`public final android.widget.RelativeLayout activityMain; `

`public final android.widget.TextView helloWorldTextView;`

A parameter will be created for each element with defined id, generating the name based on id and converting it to Pascal case. That way, it is possible to access all views and make all necessary changes, much faster and easier than findViewById or ButterKnife.

## **Binding Objects**

Another important feature implemented by the API is binding objects. Now it is possible to use instances of objects directly on the layout. First, insert the tag `<data></data>` above the root ViewGroup, then add the tag `<variable>:`

`<?xml version="1.0" encoding="utf-8"?> `

`<layout xmlns:android="http://schemas.android.com/apk/res/android" `

`    xmlns:tools="http://schemas.android.com/tools">`

 

`    <data>`

`        <variable`

`            name="mainActivity"`

`            type="com.binding.data.avenuecode.databindingexample.MainActivity"/>`

`    </data>`

 

`    <RelativeLayout`

`        android:id="@+id/activity_main"`

`        android:layout_width="match_parent"`

`        android:layout_height="match_parent"`

`        android:paddingBottom="@dimen/activity_vertical_margin"`

`        android:paddingLeft="@dimen/activity_horizontal_margin"`

`        android:paddingRight="@dimen/activity_horizontal_margin"`

`        android:paddingTop="@dimen/activity_vertical_margin"`

`        tools:context="com.binding.data.avenuecode.databindingexample.MainActivity">`

 

`        <TextView`

`            android:id="@+id/hello_world_text_view"`

`            android:layout_width="wrap_content"`

`            android:layout_height="wrap_content"`

`            android:text="Hello World!" />`

`    </RelativeLayout>`

`</layout> `

The expressions to access these object properties need to be written inside the attribute `@{}:`

`...`

`<TextView `

`    android:id="@+id/hello_world_text_view"`

`    android:layout_width="wrap_content"`

`    android:layout_height="wrap_content"`

`    android:text="@{mainActivity.helloText}" />`

`...`

The property can be accessed publicly or through getters:

`...`

`public String getHelloText() { `

`    return "Hello World!";`

`}`

`...`

## **The Takeaway**

DataBinding is a powerful API that enables development with less code, in a faster and more organized way. One disadvantage is that because the documentation is subpar, and due to be uploaded to a new library, it is sometimes difficult to find help for advanced uses. I will introduce some advanced features in future articles, and explain the advantages of using MVVM with DataBinding.

---

### Author

# Marcus Vinicius Corrêa Barcelos

 Marcus Vinicius Corrêa Barcelos is an Android Engineer at Avenue Code.

---

### Related Posts

### Building Accessible Web Applications

[READ MORE](https://blog.avenuecode.com/building-accessible-web-applications?hsLang=en-us)

### Create a Tunnel from Anypoint Platform to GCP Using an HA VPN

[READ MORE](https://blog.avenuecode.com/create-a-tunnel-from-anypoint-platform-to-gcp-using-ha-vpn?hsLang=en-us)

### Create a Classic Tunnel from GCP to Anypoint Platform

[READ MORE](https://blog.avenuecode.com/create-a-classic-tunnel-from-gcp-to-anypoint-platform?hsLang=en-us)

### How to Make an Android Splash Screen

[READ MORE](https://blog.avenuecode.com/how-to-make-an-android-splash-screen?hsLang=en-us)

### Leave a Comment!

### Avenue Code Social

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Blog/facebook.png?t=1486470796564)](https://www.facebook.com/avenuecode)

[![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Blog/twitter.png?t=1486470796842)](https://twitter.com/AvenueCode)

[![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Blog/linkedin.png?t=1486470796556)](https://www.linkedin.com/company/avenue-code)

### Newsletter

Want to stay on top of all tips and news from Avenue Code?

### Popular Snippets

![ac-logo-inverted.svg](https://blog.avenuecode.com/hubfs/Images/Logos/ac-logo-inverted.svg "ac-logo-inverted.svg")

### About Us

- [Who We Are](https://www.avenuecode.com/who-we-are)
- [What We Do](https://www.avenuecode.com/what-we-do)
- [Portfolio](https://www.avenuecode.com/portfolio)
- [Partners](https://www.avenuecode.com/partners)
- [News](https://www.avenuecode.com/news)
- [Events](https://www.avenuecode.com/events)
- [Blog](https://blog.avenuecode.com/)
- [Contact](https://www.avenuecode.com/contact)

### Our Offices

San Francisco

[+1 415 766 4178](tel:+553125161448) [ac.inquiries@avenuecode.com](mailto:brazil.info@avenuecode.com)

Belo Horizonte

[+55 31 2516 1448](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

São Paulo

[+55 11 3205 3232](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

### We're Hiring!

- [Belo Horizonte](https://www.avenuecode.com/who-we-are)
- [New York](https://www.avenuecode.com/what-we-do)
- [San Francisco](https://www.avenuecode.com/portfolio)
- [São Paulo](https://www.avenuecode.com/partners)

---

©2015 - 2017 Avenue Code

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Icons/facebook-2.png)](https://www.facebook.com/avenuecode) [![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Icons/twitter-2.png)](https://twitter.com/AvenueCode) [![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Icons/linkedin-2.png)](https://www.linkedin.com/company/avenue-code) [![Glassdoor Icon](https://blog.avenuecode.com/hubfs/Images/Icons/glassdoor-icon-1.png)](https://www.glassdoor.com/Overview/Working-at-Avenue-Code-EI_IE456173.11,22.htm) [![YouTube Icon](https://blog.avenuecode.com/hubfs/Images/Icons/youtube-2.png)](https://www.youtube.com/user/AvenueCodePlay)

/\*\* DISQUS SCRIPTS \*\*/ Please enable JavaScript to view the [comments powered by Disqus.](http://disqus.com/?ref_noscript)

© 2026 Avenue Code

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Marcus Vinicius Corrêa Barcelos",
    "url" : "https://blog.avenuecode.com/author/marcus-vinicius-corrêa-barcelos"
  },
  "datePublished" : "2017-02-01T19:01:00.000Z",
  "headline" : "Android Data Binding - Part 1",
  "image" : [ "https://blog.avenuecode.com/hubfs/Images/Blog/Android%20Binding.jpg" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.avenuecode.com/android-data-binding-part-1-2",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_LOGO%20HORIZONTAL%20group%201-7.png"
    },
    "name" : "Avenue Code"
  }
}
```