Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Binding data with the layout

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 138
    Comment on it

    Typically, to bind data with the layout i.e with the UI fields we access the view elements in the activity using findViewById() method and then set the values to the views.
    This sometimes becomes quite cumbersome when there are large no. of views which we want to access.

    To remove this extra code and to make the activity and Layout more cohesive, we can use Data Binding in Android.


    In Data Binding,
    1. we have to specify Model Class which we will use to set the values inside the layout xml file.
    2. We specify Model class' Instance variables which we want to set to each of the view inside the layout xml file.
    3. We just have to pass the Object of the Model class whcih contain the data using Data Binding methods.


    This helps in removing those extra findViewById() calls and the setText(), setSrc() (and other) method calls.


    Follow the below steps to add data binding in your app:


    Add the below line of code in your build.gradle file (app level) to enable data binding:

    android {
        dataBinding {
            enabled = true

    Create a Model Class; suppose you want to show student details create the following class:

    public class Student {
        public String firstName;
        public String lastName;
        public String id;
        public String mClass;
        public double percentage;
        public Student(String firstName, String lastName, String id, String mClass, double percentage) {
            this.firstName = firstName;
            this.lastName = lastName;
   = id;
            this.mClass = mClass;
            this.percentage = percentage;

    Now to bind this model class with the layout,  create a layout activity_main.xml like this:

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="">
                type="" />
                android:text="@{student.firstName}" />
                android:text="@{student.lastName}" />
                android:text="@{}" />
                android:text="@{student.mClass}" />
                android:text="@{String.valueOf(student.percentage)}" />


    To bind data in a layout, you've to add layout elemt as a root which is followed by data tag in which you'll specify the model class and a variable name.
    data tag is then followed by view root element which would have been a root you've not binded the data in a layout.


    Now you can set the values in the UI elements by using variable specified in data element.
    For ex. to @{student.firstName} will return value stored in firstname variable.


    In the last TextView element I've used @{String.valueOf(student.percentage)} to set the value as percentage variable is of type double and I'll have to first convert it into string to set it into TextView.


    We have now bind the model class  with the layout. To add values inside this layout, use the following code inside your activity:

    protected void onCreate(Bundle savedInstanceState) {
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        Student student = new Student("Aneesh", "Garg","ID001","Intermediate",79.9);


    As the layout's name is activity_main.xml, so the setContentView will return object of class ActivityMainBinding.
    Now to set values in the layout pass the object in setStudent() method.
    setStudent() name is derived from the variable name we specified in the name attribute inside variable tag i.e  name="student".

    Hope it helps! :)


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: