Data Binding technique android Part-I

Updated 15 December 2016


With the relaese of android M , the data binding came into android. As its a support library it supports back to API level 7. The data binding is a technique to bind the application logic to the layouts which was earlier a redundant work. You have to initialize the view using findViewById() and then you have to set the value for the view.

But with data binding you can bind your data directly to the xml without initializing them to variables. So here we will make a Sample project to understand data binding.

Firstly we have to support the binding, to do so add these lines of code to your project level gradle

now add plugin to your app level gradle

Now your project is ready to support databinding. So lets start binding the data to our layout.

Suppose we want a layout to show a person data having name, age , phone number, email etc. For this we are having a Person class as

So the binded layout activity_main.xml will be like

With this xml file, the system will generate a Binding class with converting the xml name in Pascal case and suffixing Binding in it. As our xml name is activity_main so the binding class wil be named ActivityMainBinding.

Lets inflate the xml and get our data as required

Initially it will show an error(red mark) but once project build is done it will work as expected. Basically data binding reduces the redundant and non logical code. You can use databinding with listview and recycler view as well and the places where you are inflating a single xml again and again. But we will discuss it later.


. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home