Or stuff you won't see written somewhere else :)

It's been a while since i've written a post and now with the new blogging system I should be getting back to writing about our Mobile/Android life :)

My post today will tackle a few unwritten tips you need to know when using the new Android data binding Library.

Let's start by reviewing some background information to all of you about the Android data binding Library.

What is data binding in general

From wikipedia:

Data binding is the process that establishes a connection between the application UI (User Interface) and Business logic. If the settings and notifications are correctly set, the data reflects changes when made. It can also mean that when the UI is changed, the underlying data will reflect that change.

So in a short sentence, data binding allows us to bind(create a link) between the UI to the Logic. This helps us later when changes in the UI or in our data occur and we have a simpler way to reflect them.

Data binding Library in Android

The Data Binding Library was built as a support library, so we will be using it on almost all Android platform versions, starting Android 2.1 (API level 7+). Just a heads up - because data binding is still in beta, API may change and may not correlate with the written documentation.

While trying out data binding in Android this post will be updated when we find any interesting findings.

Findings

  • Setting up data binding in Android studio : First we need to make sure we have Android Studio 1.3 or higher. Then we need to edit a few gradle files which allow us to get data binding support up and running.
   buildscript {
       repositories {
           jcenter()
       }
       dependencies {
           classpath 'com.android.tools.build:gradle:1.5.0-beta1'
           // NOTE: Do not place your application dependencies here; they belong
           // in the individual module build.gradle files
       }
   }

This goes to you root top level build.gradle file. In your app module build.gradle file you need to make sure data binding is enabled.

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    dataBinding {
        enabled = true
    }
  ...    
  • Creating POJO for binding - There are a few design choices we can use. we chose the Model-View-ViewModel pattern.

    Model-View-ViewModel image

    Simple enough to start with.

  • Edit and update layouts - Here is our actual work with the new added data binding feature, We will go over the layout files where we need to (according to our data model and View model). We are adding meta-data and markup allowing the
    binding code, later on, to work some magic :)

  • Data binding declaration - After the layout markup we dive into our code and start adding data binding declaration to specific places.

The source code for this sample is up on Github.