Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Data binding in your xml layouts

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 142
    Comment on it

    Data Binding Guide

    This document explains how to use the Data Binding Library to write declarative layouts and minimize the glue code necessary to bind your application logic and layouts.

    Setting Up Work Environment:

    dependencies {
           classpath "com.android.tools.build:gradle:1.3.0-beta4"
           classpath "com.android.databinding:dataBinder:1.0-rc1"
       }
    

    In each module you want to use data binding, apply the plugin right after android plugin

        apply plugin: 'com.android.application'
        apply plugin: 'com.android.databinding'
    
    

    BASIC REQUIREMENTS

    Make sure you are using a compatible version of Android Studio. Android Studio 1.3

    EXAMPLE :

    Layout: activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
    <data>
            <variable name="user" type="com.example.devesh.myapplication.User"/>
        </data>
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user.firstName}"/>
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user.lastName}"/>
    
        </LinearLayout>
    </layout>
    

    Model Class USER :

    package com.example.devesh.myapplication;
    
    /**
     * Created by devesh on 21/9/15.
     */
    public class User {
        public final String firstName;
        public final String lastName;
        public User(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
    }
    

    MainActivity : how to use data binding

        public class MainActivity extends AppCompatActivity  {
    
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setDisplayUseLogoEnabled(true);
                setContentView(R.layout.activity_main);
    
            // note  ActivityMainBinding refers to layout file activity_main
    
                ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
           // only you need to set this user ,this will automatically reflects in your layout dynamically.  
              User user = new User("hello", "devesh");
    
                binding.setUser(user);
            }
    
    

    Data will dynamically get set to your layout.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: