Data Binding / Observable

Data Binding – Part 5

At Google I/O 2015 a number of new Android libraries and tools were announced. One of them was the all new Data Binding library and in this series we’ll take a look at the library and explore some of the powerful features it provides.

We’ve covered some pretty useful aspects of Data Binding so far but there is a feature that makes it even more powerful still: Observables.

Observables are really useful when it comes to data which may change over time. To demonstrate this let’s pretend that the Twitter API does not actually return the quoted status within the twitter4j.Status object and we need to perform a separate network call to retrieve it (Twitter doesn’t work like this, but indluge me!). It would not make sense to fetch these up-front, we should only retrieve those that the user wants to retrieve by tapping on the quoted status. We wired the click handler previously – so we would then need to retrieve the quoted status and then update the UI once it was retrieved.

Observables remove the need for us to have to refresh the UI manually when the underlying data changes. As the name suggests an Observable can be observed by another component and that component will receive a callback whenever the Observable changes.

While we can make the entire ModelView class an Observable we can also make individual fileds Observable for use-cases where only a part of the data is likely to change.

Let’s extend our ModelView class to add an additional field plus some methods whihc will allow us to change it:

We already created the field representing the quoted status and we used this previously to enable us to only display the quoted text box when the item has a quoted status, but we didn’t actually expose the quoted status data itself.

Next we’l change our click handler to toggle the contents of the Observable between set and null. If we did actually have to perform a network call here we would initiate it, update the UI to indicate a loading state, and then wait. When the network call completed we’d simply update the contents of the Observable.

Finally we’ll add the additional Views required to display the quoted status. These are pretty much identical to the ones we already have to display the main status – they are just bound to the values within the Observable instead. We also have some visibility bindings to toggle the visibility based on whether the Observable contains data or a null .data.Status object:

That’s it – we don’t need to do anything more. The UI will automatically update whenever the Observable changes:

That completes our basic overview of the new Data Binding library. I must confess that I was initially skeptical when the Data Binding was announced as it immediately reminded me of JSP (and I still have the emotional scars from developing those). It is for this reason that I advocate keeping the actual logic outside of the layout files as much as possible. However having had a play with it for this series I am pleasantly surprised and it seems to do an awful lot right. Once it exits beta, I would certainly consider using it on commercial projects.

The source code for this article is available here.

© 2015, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Data Binding – Part 5 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at


  1. Thanks for highlighting and explaining the observable feature of data binding. (It’s yet another aspect of data binding I had no clue about.)

    Also, thanks for offering optimism on the general viability and quality of data binding. I want to believe (but, as we all know too well, sometimes a new Android SDK feature doesn’t quite merit our confidence…)

  2. Hello

    Nice tutorial.

    I am also trying to implement data binding concept. I would like to fetch the value form model which contain list of another model and want value from that.
    for e.x.
    Lets say Status model has one property arrayList , image contain Url and image name and i am passing status model to my xml layout. so how can i get value from arraylist of Images.

    Please help me out. I am stuck with this.

Leave a Reply

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