Android Data Binding

I wanted to start a series of tutorials on using Android Widgets but as I have not done any Android development for a few months I figured I’d better refresh my knowledge. I quickly discovered that there was a new(?) technology call Data Binding that allowed for declarative layouts. It looked interesting. I started following the guide and became very frustrated as it didn’t seem to work. After many hours of shouting and throwing things I finally figured out how to get it to work. But before we go on I realise that blogs are somewhat time sensitive so just in case Google update their docs, the first place you should try is their official guide:

Android Data Binding Guide

Now, if my information is accurate Android Data Binding is not actually a part of Android per se, it is part of Android Development Studio and comes in the support library. I don’t actually use anything but Android Developer Studio so I can’t comment whether this will work with something like Eclipse. But lets begin. First off make sure you have updated to the latest support packages and SDK’s etc.

Android Developer Studio

I think the first problem we have to deal with is getting Android Data Binding library all linked up. I found there is nothing wrong with the documentation so let’s begin.

Open up Android Developer Studio and create a new project

Android Developer Studio New project

You can name yours whatever you like but maybe to ensure now errors keep it the same as mine.

On the Targeted devices screen, I have just left everything as default. As the library is not part of Android(?) it will work from Android 2.1.

Android Targeted Devices

Ok so on the next screen you need to select Empty Activity. This is very important and will help prevent hair loss!

empty activity

If you choose Blank Activity, Android Studio will create two XML files, one called activity_main.xml (assuming you leave the default) and one called content_main.xml. For the purposes of this tutorial you do not want this. If you do choose the blank activity by mistake simply delete the to layout files and create a new one.

gradle.build

Your project has two gradle.build files: one that relates to the entire project and one that relates to the module.

gradle build files

Open up the project one and add the following:

class path for data binding

Make sure that you are running 1.3.0 on the gradle path.

Now open up the module gradle.build file and add:

apply plugin

Save and then you should see a message stating that the gradle files have been updated and a link to sync the project.

If you followed me exactly you will get an error. I obviously didn’t mean to make an error but the fact is that you learn a lot from error so I have left it in. The problem is just a typo. If you read the error you should be able to figure out whats wrong but if it still alludes you the issue is in the project gradle.build file. You’ll notice I put a comment stating what the current version is and I put 4, but in the actual path I put 14. Delete the 1 and then click try again and you should find everything is ok.

POJO

The data that we are going to bind to is just a plain old java object. As i am in the middle of converting one of my exceptionally trivial IOS apps to Android, we will use this and create a Payment class. For now it will be very simple. So create a new class and call it Payment.

pojo payment class

So you can see that I have created one variable called noOfSingles and given it accessors and I have also created a constructor to set the initial value. That’s it for now.

activity_main.xml

For the purposes of this tutorial we are going to keep to the format that Google use in the guide so open up the layout file and delete what is there and replace with the following:

basic layout

Now we have a basic layout with a TextView displaying hello world. Now we need to add the following above the declaration for the linear layout:

What we have done is declare a new variable of the type of our payment class. Now we are going to use it to bind noOfSingles to our TextView. But before we do we need to alter our class a little:

Our getNoOfSingles previously returned an int but we need it to return a string. We change the return type to String and add String.valueOf() to our return variable. This is just for the purposes of this tutorial.

Ok now back in our activity_main.xml file we change the value of “hello world” in our textview to @{payment.noOfSingles}. The finished product should look like this:

finished layout

MainActivity.java

In or onCreate overridden method we add the following bellow the boilerplate code:

If you followed the Android Data Binding Guide published by Google you may be getting errors highlighted by Android Developer Studio at this point. The reason for this is that in their code example the layout file is called main_activity.xml but yours is probably called activity_main.xml and the ActivityMainBinding class is derived from the name of the file – it even adds an import for you:

but obviously only if you have the right name for your layout file. So make sure it is correct, also in the setContentView.

Now we simply instantiate our class:

and then bind it:

Now if you run the app:

binding app working

Voila, we have a bound value in our TextView.

In the next article we will explore some more complex binding examples

1 thought on “Android Data Binding

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.