Toilet Paper Android Data Binding

Android Data Binding

Problem: Glue Code in Android Apps

In many Android apps, the UI code calls findViewById to find views from the layout of a screen. In a next step, these views are downscaled into the needed type (e.g. TextView) and finally, views are called to manipulate them (e.g. setText).

This (unnecessary) "glue code", which binds UI to the models, is usually located in the largely complex activities and fragments.

Solution: Google's Android Data Binding Library

With the help of Google's Android Data Binding Library, this boilerplate code can be eliminated. Model classes (e.g. POJOs) may be referenced directly from the XML layout, to set e.g. Drawables and StringResources directly in the views.

Using ObservableFields or BaseObservables, the layout can even bind to the properties of the models and apply changes automatically.


<?xml version="1.0" encoding="utf-8"?>
<!-- Wrap your Layout into <layout></layout> tags -->
<layout [...]>

    <!-- define variables and imports you want to use -->
        <variable name="model" type="de.schroepf.databinding.model.User" />

    <LinearLayout [...]>

        <!-- Surround any data binding expression with @{...} -->
        <ImageView [...] app:imageResource='@{}' />
        <TextView [...] android:text='@{model.firstName + " " + model.lastName}' />
// The fields of this model class can be accessed in the layout.xml file 
// Extend BaseObservable or use ObservableFields to allow the layout to respond to changes.
public class User {
    public final String firstName;
    public final String lastName;
    public final int photo;

    public User(String firstName, String lastName, @DrawableRes int photo) {
        this.firstName = firstName;
        this.lastName = lastName; = photo;
public class MainActivity extends Activity { 
    // represents the state we want to display in this activity
    private User myModel = new User("Coffee", "Toffee", R.drawable.coffee_toffee);

    protected void onCreate(Bundle savedInstanceState) {

        // Make sure to use DataBindingUtil to setContentView/inflate your view and retrieve the binding
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        // use the binding to access the layout's view hierarchy (typesafe!) and set variables

Further Aspects



Tobias Schroepf / Senior Software Architect / Business Division Automotive World

Download Toilet Paper #84: Android Data Binding (pdf)

How to eliminate boilerplate code in Android apps

Cookie Settings

This website uses cookies to personalize content and ads, provide social media features, and analyze website traffic. In addition, information about your use of the website is shared with social media, advertising, and analytics partners. These partners may merge the information with other data that you have provided to them or that they have collected from you using the services.

For more information, please refer to our privacy policy. There you can also change your cookie settings later on.

contact icon

Contact us now