The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


The Data Binding Framework

To quote Rudyard Kipling:

East is East and West is West, and never the twain shall meet

In many programming environments, including classical Android development, one could paraphrase Kipling as “models are models and views are views, and never the twain shall meet, except by means of some controller or presenter or something”. The result is a fair amount of code that populates views with model-supplied data and updates those models as the user alters the data in the views (e.g., types something in an EditText widget).

Data binding, in general, refers to frameworks or libraries designed to help simplify some of this data migration, where the definitions of the models and views can be used to automatically “bind” them without as much custom controller- or presenter-style logic.

Interest in data binding spiked in 2015, when Google released the first beta editions of data binding support via Android Studio, the Android Gradle Plugin, and a new data-binding support library.

This chapter explores Google’s data binding support and how to use it to simplify your Android app development.

Prerequisites

This chapter requires that you have read the core chapters of this book. In particular, the sample apps are based off of samples from the chapter on Internet access. Also, some samples use RecyclerView.

The What, Now?

In that chapter on Internet access, we examined a few variations of a sample app that retrieved the latest android questions from Stack Overflow and displayed them in a ListView. Our QuestionsFragment had an ItemsAdapter for populating the ListView, complete with a typical getView() implementation:

    setListAdapter(new ItemsAdapter(questions.items));
  }

  class ItemsAdapter extends ArrayAdapter<Item> {
    ItemsAdapter(List<Item> items) {
      super(getActivity(), R.layout.row, R.id.title, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View row=super.getView(position, convertView, parent);
      Item item=getItem(position);
      ImageView icon=(ImageView)row.findViewById(R.id.icon);

      Picasso.with(getActivity()).load(item.owner.profileImage)
             .fit().centerCrop()
             .placeholder(R.drawable.owner_placeholder)
             .error(R.drawable.owner_error).into(icon);

      TextView title=(TextView)row.findViewById(R.id.title);

      title.setText(Html.fromHtml(getItem(position).title));

Some parts of getView() are clearly distinct for this application, notably using Picasso to download the question asker’s avatar and using Html.fromHtml() to handle HTML-style entities in the title.

However, the general process used here in getView() is fairly rote:

Data binding, as a general technique, aims to reduce that rote coding by declaratively telling a framework how to pull data from model objects (e.g., instances of Item) and pour that data into widgets (e.g., ImageView and TextView).

The Basic Steps

The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).

The Extended Layout Resource

The preview of this section is being chased by zombies.

The Binding Expression Language

The preview of this section was lost in the sofa cushions.

Observables and Updating the Binding

The preview of this section is presently indisposed.

Two-Way Binding

The preview of this section apparently resembled a Pokémon.

Other Features of Note

The preview of this section was lost in the sofa cushions.