Android Data Binding - Say Goodbye To FindViewById
Wednesday, 03 June 2015

Something new that comes with Android M that has almost gone unnoticed is the Data Binding library. It could mark the biggest change in Android programming since the introduction of the Fragment.

 

androiddevicon

The big headline news about Data Binding is that it makes it very much easier to implement an MCV architecture in an Android app. This is profound, but first I want to mention a small improvement that will affect all Android programmers irrespective of whether or not they use the MVC approach. As the new feature is implemented as a support library you can even make use of it when targeting earlier versions of Android. 

How many times in an Android program do you use FindViewById to hook up the code and the XML markup?

My guess is that it is such a boilerplate that you could type the whole thing, the assignment the cast and the call, in your sleep. 

You must have wondered why Google didn't introduce an automatic connection between the View id that you define in the XML layout file and a variable used to hold a reference to the View object created by the inflater? After all .NET manages it and even JavaScript/HTML5 sort of does it. 

Now with the Data Binding library you can automatically have UI elements that have ids automatically converted to public variables in an autogenerated binding class. This means you don't need to use FindViewById any more!

For example, if you have a TextView in your Data Bound XML layout:

<TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/lastName"/>

Then after the binding object has been generated you can access the TextView using: binding.lastName. That is, the binding class has the variable:

public final TextView lastName;

and this is automatically set to reference the TextView object with id lastName

The new Data Binding library extends the XML markup language so that it can establish a connection between variables in your code and the View objects it specifies as well as the other way round. This makes Android XML markup very much more like the fuller featured XAML from Microsoft. 

For example:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android=
 "http://schemas.android.com/apk/res/android">
 <data>
  <variable name="user"
            type="com.example.User"/>
 </data>
 <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
  <TextView android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{user.firstName}"/>
   </LinearLayout>
</layout>

 

First notice the use of the new <layout> tag which makes the layout into a data-binding layout file. Next we have the <data> tag which is used to list the variables in your code that are going to be bound to the UI elements. In this case only one variable is declared - user which is an object of type User. 

After the <data> you can have a standard layout file but you can now use @{expression} to define the value of any property of the UI element. The expression can make use of the variables you defined in the <data> tag. So in this example the text property of the TextView will be automatically set to user.firstName. Of course there has to be an object called user with a firstName property in your code for this to work.

To make the binding work you need to change the way you inflate the layout. 

ActivityMainBinding binding =
 DataBindingUtil.setContentView
               this, R.layout.main_activity);

This returns the binding object. You can use this, as described earlier to access any of the View objects that have ids in the layout file. You can also use it to bind variables in your code to the generated View objects:

binding.setUser(user);

where user is an instance of the User class in your program. In the example above this would set all of the UI properties that made use of properties in the user instance. There is also a generated getUser that can transfer properties from the UI to the code. 

You can also set things up so that the if the properties of the instance change then the UI properties are automatically updated and vice versa - two way binding.

The argument goes that with data binding you don't really need to give UI elements ids any more because you can access them via the getters and setters in the binding object - but either way the tedious FindViewById days are well and truly over.

You can use the new library with the help of Android Studio 1.3 preview and you can use it with with versions before Android M. However, it is important to know that at the moment the whole thing doesn't work reliably, the documentation isn't particularly helpful and unless you have time to waste it is better to leave things until at least the next, or even the next-next, release.

androiddevicon

More Information

Data Binding Guide

Related Articles

Android M And A New Android Studio

Android M To Appear At Google I/O?

Google I/O 2015 Announced

Android L Is Lollipop And New Nexus Devices

Android OS History

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

Banner


GR00T Could Be The Robot You Have Always Wanted
27/03/2024

We may not have flying cars, but we could well soon have robots that match up to predictions for the 21st century. Nvidia has announced GR00T, a cleverly named project to build robots using foundation [ ... ]



White House Urges Memory Safe Software
29/02/2024

The White House is urging developers to adopt memory safe programming languages, suggesting Rust would be a safer choice than C or C++. 


More News

 

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Tuesday, 04 August 2015 )