Android Data Binding – Part 2

In Part 1 I gave an introduction into the Android Data Binding Library. I suggest reading that post first before reading further.

In this post I am going to discuss the following :

  • Binding Adapters
  • Binding Events
  • Two Way Binding

Binding Adapters

Creating an adapter and using Android Data Binding for each item is quite simple. Below is an example of how your RecyclerView adapter will look after using Data Binding:

  1. The first difference in this method is that our ViewHolder  object now only contains a ViewDataBinding  object (instead of ImageViews and TextViews)  and a method to get the binding object.
  2. The method onBindViewHolder binds the view to the item variables. This is shown by the lines holder.getBinding().setVariable(, bookDetail) . This sets the views book variable defined in the layout XML.
  3. The method holder.getBinding().executePendingBindings()  triggers the View to be updated with the new values provided. This method has to be run on the UI thread.
  4. Within our list_item_book.xml  layout file we define and access variables in the same way as in Part 1. With the <data> section defining all the variables to use in the xml, including the imports. The Views then contain reference to those objects. For example  android:text="@{book.bookTitle}"  accesses the books title and assigns the value to the text of the TextView.


In the same way that we bind variables in XML, we can set variables for click listeners in XML.

  1.  Create the click listener in code:

2. In list_item_book.xml, define the View.OnClickListener  and set the View that the click should fire on by using the variable defined android:onClick="@{click_listener}" . This will then trigger the click event.

3. In the adapter, set the click listener:

I ran into an issue whilst trying to set the Views Tag in XML due to a bug in the Android Data Binding Library (they did warn us it’s in beta 🙂 )

The following XML causes an issue with the Data Binding Library that will cause your app to not compile:

An explanation from StackOverflow as to why this wont work:

” When targeting devices pre-ICS, Android data binding takes over the tag of the outermost element of the layout. This tag is used for mostly for binding lifecycle and is used by DataBindingUtil.findBinding()  and DataBindingUtil.getBinding() . “

Following the advice from the post and because I am targeting ICS and above,  I used the mechanism specified in the answer:

Create a BindingAdapter to set the tag after the binding has occurred:

Then in the XML, I can easily attach any object onto the View:

When the item is clicked we can get more information about the item by using the  view.getTag()  mechanism.

Two Way Binding

This is where things get tricky as the library does not work as expected. Unfortunately, binding an EditText to a String field in an object doesn’t yield the results we would hope for.

The following is a way to get two way binding working:

  1. In our BookDetail object we create an ObservableField<String>  that will contain the books title. This will then inform anything that is watching of variable changes.
  2. In our XML, we will bind to that field on both the EditText and the TextView.
  3. We need to create a TextWatcher which will update the String field when the user types. It will notify the TextView of the changes.

As you can see this is not an ideal solution. Firstly a lot of boiler plate code is required to get it to work and it’s not very elegant.

Better Solution

Fabio Collini has a great detailed post with his solution to this problem – by creating a custom binding for the property. In a few simple steps:

  1. Create a custom BindableString  object
  2. To get the text out of the BindableString  object, we need to have a binding conversion. This instructs views on how to convert a BindableString into a String object:
  3. Create a custom BindingAdapter to handle the text watching and setting:
  4. Set the bindable string in XML.
  5. Change the BookDetail object to use a BindableString instead of the ObservableField<String>

6. Two way binding should now work within your app.

Final Verdict

The Data Binding Library is a powerful tool. I suspect it will become more useful and more widely adopted. For now though, there are a few bugs and some kinks that need to be ironed out.  After experimenting with it for a while, I am excited for what the library could potentially do in the future.

What are your thoughts on this library? Have you used it yet? Leave a comment below!


Getting Started: The Android Data Binding Library Part 1

Anyone who has dabbled a bit in Android Development will know that Android is lacking a key component of MVVM: The ability to bind data without a couple of lines of code. 

For instance, setting a TextView to some value retrieved from a server is pretty complex, considering the simplicity of the task:

Doing everything this way can become a bit cumbersome and leads to very cluttered code. I don’t think anyone needs much convincing that this process of manually binding data is not ideal.

Cue: Android Data Binding Library (It’s like someone heard our cries? :-)). The 6 lines above can be converted into 2:

I decided to take a look and try it out, and see how well it would work for my applications.

Initial Impressions:

  • Very powerful library 
  • Less cluttered code
  • Less view logic in Activity and Fragment classes (yay!?!)
  • Compatible from API version 7+ (2.1)
  • Not recommended for use in production yet (It’s currently in Public Beta Phase but you are welcome to ship your app using it, it’s just not advised)
  • No support in Android Studio for syntax highlighting or code completion 

How to get started with the Android Data Binding Library:

  1. To get started with the Data Binding Framework, you need to include a couple of Gradle Dependencies in your top level build.gradle file in the dependencies section:

  2. In the Top Level build.gradle make sure the following is defined (It probably is already):
  3. In each module of your project make sure to include the following right after the android plugin is applied:
  4. Create your object that you wish to represent in your view. In our case we have a Page object which contains a number, image link and some text.
  5. Create Layout XML as per usual.  We have a ImageView  and a TextView  in our layout. At the start of the layout we will define what objects and methods will be used in this page.

    Let’s go through the new changes in this XML:

    • As you can see we no longer use a LinearLayout as the start tag, instead we use <layout> with the namespace declarations.
    • There is now a <data> section at the top of the file, which will include your imports to the different libraries you require. In this case, I required the TextUtils class. You can access any static methods define in a class. In this example I use the method TextUtils.isEmpty() to do different view logic.
    • <variable> tag defined, this allows you to define objects within the XML that will be used on the View itself.
    • Accessing a variable and some of its properties is simple:

    The words @{page2.htmlText}  will run the method getHtmlText()  on the page object.  One thing to note: Android Studio looks as if the method getHtmlText()  is unused, since it hasn’t found the usage in the XML but after deleting it, the project will fail to compile.

  6. After creating the Page object and the XML that will use the object, how do we go about putting the two together? Android will generate the object FragmentPageBinding (this name will change depending on your layout, eg. activity_book.xml  will create the object ActivityBookBinding ) which contains all the objects that you define in the <data> section of your corresponding XML file. From then, you just set the variables and your view will contain the correct values. As you can see, no view logic is sitting in my Fragment or Activity. Yay!

  7. In the above code, you may also notice that on the ImageView we have the attribute  app:imageUrl="@{page.image}" . I like to load images from the web, in a background thread – using a library like Glide or Picasso . Create a Utils class called BindingUtils and pop this method in there. Now your ImageView will call this section of code with the url that we passed to it. We can then do what we like with it. In this case, we load the image into the view.

In Part 2 we will look at Two Way Binding and Binding Adapters.

Four Apps I Can’t Live Without

I’m always really curious to know what apps people use on a daily basis, and what about those apps makes them so awesome?

I have a couple of apps that have just slotted so easily into my everyday life, that I don’t even realise I am using them.

Here they are:



This app is integral to using an Android phone, if you haven’t installed this app, you are not taking advantage of all of your phone’s capabilities.

Basically it allows you to see notifications from your phone, on your computer. It pushes notifications to your computer and you even have the ability to interact with some of the notifications. As an example when you are using TuneIn Radio on your phone, you can pause and play the music on your phone from your computer. You are even able to see your phone ringing, on your computer. Rad right?

The app also allows you to easily send links, text or files to your devices from your computer using Google Chrome. Gone are the days of emailing yourself links & files to get them on your phone. The Chrome extension also allows you to send SMS messages from your computer on your phone (if you are all old school cool and like sending a good old text message).

This is definitely one of my favourite apps, and I feel like Google should include this functionality in their operating systems by default, because it’s so cool.

PushBullet is also really easy to set up: (

Android Store Download

iTunes Store Download



You know those painful tasks that should be easy but aren’t? Like every time you post a photo to Instagram, you have to go to Twitter and upload the same picture, just so it will appear nicely in your Twitter feed?

Well IFTTT can handle these kinds of things for you. The whole concept is “If this, then that”. Basically, you can create recipes (or use ones already created by other people) that will result in an action if certain conditions occur. As an example, you can set the app to save all your instagram photos to dropbox, which means that every time you upload a new photo to instagram, it will also save that photo in a specific location on dropbox, without you having to do anything.

This app really appeals to the developer in me. You can also buy these cool Philip Hue Lights that can turn on when you arrive home. My only gripe with this app is that it is a bit buggy, sometimes the recipes don’t run and that makes me a bit sad.

App Store Downloads:

Android Play Store Download

iTunes Store Download


Apparently all companies have decided that Microsoft Exchange Mail is the best thing ever. I tend to disagree, I feel like we should all be using Google Apps for business, but for those of us who are subjected to using Exchange Mail every day, this app is really useful.

It is one of the best mail apps that integrates quite seamlessly with an exchange server. I love the look of the app, it’s stability and reliability. You can also connect multiple accounts of different types: Gmail, Exchange, Outlook, Yahoo, Office 365 , iCloud.

Best of all? The app is free and doesn’t have any adverts. One thing that is not so cool, you can’t view your calendar and event invites are still a bit weird in the app. But nevertheless, a great alternative to Microsoft’s attempt at Outlook for Android.

App Store Downloads:

Android Play Store

iTunes Store Download

TuneIn Radiotunein

I hate downloading music, especially because you need to know what the song names are and then there is the whole effort of finding the songs online (usually on some obscure website, with a million ads).

It frustrates me, so I normally end up listening to the same songs over and over again. Introducing TuneIn Radio. I never need to download another song again, or rip MP3s from a CD (Is this even still a thing? I mean, how old school can you get?)

TuneIn radio, has a massive list of radio stations you can choose from, that play all different types of music. Including local stations such as 5fm, 94.7, Metro FM. My current favourite – Capital London. There is a wide variety of stations to listen to, and every music taste is catered for (Including some delightful tunes by Tibetan Throat Singers)

App Store Downloads:

Android Play Store

iTunes Store 


What apps can’t you live without?

South African Lotto Results & Checker {ANDROID APP} – DEPRECATED

UPDATE : 11 September 2015 : Unfortunately I had to discontinue this service as the official Lottery Provider changed, and the results are not published in the same manner. Sorry!

After searching the Google Play store for a Lotto app, I realised they were all pretty revolting, so I decided to create my own one – and here it is!

The application provides the latest South African National Lottery results for Lotto, Lotto Plus and PowerBall.

South African Lotto Results


Features of the application:

– View Latest Results for PowerBall, Lotto and Lotto Plus
– Generate Quick picks for Lotto and PowerBall which can be easily shared.
– Check tickets for Lotto, Lotto Plus and Powerball for any draw date.





You can download the app here: 

Below are some screenshots of the application.

Application Screenshot   Application Screenshot

If you have any ideas for improvements or suggestions I would appreciate them. I have a few more ideas up my sleeve for the application, so watch this space!