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.

Example

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

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

    <LinearLayout [...]>

        <!-- Surround any data binding expression with @{...} -->
        <ImageView [...] app:imageResource='@{model.photo}' />
        <TextView [...] android:text='@{model.firstName + " " + model.lastName}' />
    </LinearLayout>
</layout>
// 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;
    @DrawableRes
    public final int photo;

    public User(String firstName, String lastName, @DrawableRes int photo) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.photo = 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);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(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
        binding.setModel(myModel);
    }
}

Further Aspects

---

Author

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

Wir verwenden Cookies, um unsere Webseite für Sie zu optimieren. Mit dem Besuch unserer Webseite erklären Sie sich damit einverstanden. // Our website is using cookies to improve your experience. By continuing to browse the site, you are agreeing to our use of cookies.

Weitere Informationen finden Sie in unserer Datenschutzerklärung. // For more information, please refer to our privacy policy.