Let’s implement Android Data Binding library in your android app: Part-1

Now there is no need for the third party libraries to bind views and data for better coding, because android recently introduced a new feature for this.

Android Data Binding is one of the most interesting feature recently introduced but still in beta version.

Android Data Binding exist between app presentation layer and data model layer that holds the information to show.

You can implement android data binding just in few steps.

To integrate binding we need to use Gradle 1.5.0 or above. Let’s update build.gradle file of the project by adding the following string:

dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
    }

To enable data binding in your project edit your build.gradle(app level)

just put this code in android tag:-

android{
 ....
   dataBinding {
       enabled = true;
   }
}

Here is the code for build.gradle(app level)

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    dataBinding {
        enabled = true;
    }
    defaultConfig {
        applicationId "sample.android.databinding"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:25.3.0'
}

The next and very important step add <layout > tag as root in your xml.

activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="sample.android.databinding.MainActivity">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="20sp" />

    </RelativeLayout>
</layout>

Now Android Data Binding library will handle rest. It will generate a binding class with your xml. All the widgets that you have added in your xml with there id’s (android:id=”@+id/textView”) have been added to ActivityMainBinding class.

Next step go to your main activity (find the code below):-

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding activityMainBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activityMainBinding = DataBindingUtil
                .setContentView(this, R.layout.activity_main);
        
        activityMainBinding.textView
                .setText("Android Data Binding");
    }
}

Yes just a few line of code and you are ready with Android Data Binding.

Thanks for reading this short story about Android Data Binding library.

We will take this up to the next level with click listeners and declaring variable in xml with the help of Android Data Binding library.

 I would love to hear your own recommendations and experiences in the comments below. Please share your Valuable feedback.
Advertisements

3 thoughts on “Let’s implement Android Data Binding library in your android app: Part-1

  1. Nyanna says:

    Well as a lay man, what I understand is that the carbon in the air does not let sunlight escape once it enters the atmosphere. So, because carbon holds on to sunlight, it increases the atmospheric teeptraeurm. At the same time too much carbon blocks sunlight, and so it brings about a dimming phenomena.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s