Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Dynamic DataBinding In Android

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.02k
    Comment on it

    This Blog simply illustrate how to use dynamic data binding in android

    Tired of writing same code again and again for finding views and setting data into it ?

    here is a simple solution which will now reduce the developer's effort for doing same task such as setting data into views, either static data or dynamic, So lets start step by step guide for dynamic data binding.

    Step 1 : Add following snippet of code into you app under android tag gradle file.

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

     

    Step 2: Create a bin class which will hold the data going to be used in your app, Ex.

    /**
     * Created by amitrai on 9/5/16.
     */
    public class Shop {
    
        private final String ShopName;
        private final String ShopAddress;
        private boolean isOpen = false;
        private boolean isClose = false;
        public Shop(String ShopName, String ShopAddress) {
            this.ShopName = ShopName;
            this.ShopAddress = ShopAddress;
        }
        public String getShopName() {
            return this.ShopName;
        }
        public String getShopAddress() {
            return this.ShopAddress;
        }
    
        public boolean isOpen() {
            return isOpen;
        }
    
        public boolean isClose() {
            return isClose;
        }
    
    }

     

    Step 3:-  Now inside your layout file create a root tag named layout and add data tag at the top, eg.

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
        <data>
            <variable name="shop" type="android.com.demo_dynamicdatabinding.Shop"/>
        </data>
        <RelativeLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent">
            <LinearLayout
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@{shop.shopName}"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{shop.shopAddress}"/>
    
    
            </LinearLayout>
        </RelativeLayout>
    </layout>

    Explanation :- Inside data tag there is a tag named variable which creates the instance of the bin class and that instance is used by the views inside the layout for example.

    
    <data>
            <variable name="user" type="android.com.demo_dynamicdatabinding.User"/>
    </data>
    
    <LinearLayout
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
              
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@{shop.shopName}"/>
    <!--
                here this line "@{shop.shopName}" is setting data from the bin class to the view without
                the need of any other java code
    -->
    
            </LinearLayout>

    This way data is being set from bin class to the text view.

     

    Step 4:- Inside your Activity in my case its main activity do the following,

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
           // ActivityMainBinding is an auto generated class.
            ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    
           // Initializing the bin class object which will be reflected in our view.
            Shop shop = new Shop("My shop", "address is dehraoon");
    
        // setting data in view
            binding.setShop(shop);
          
        }

    In above code snippet There is an auto generated class named ActivityMainBinding, this name comes from the layoutfile name for the activity removing underscores and adding suffix as Binding, So if your layout file name is xyz_activity your generated file name will be XyzActivityBiniding.

    That's all you need for getting started with dynamic data binding go ahead and try this.

    You can check out the demo application for the same from this link.

    Similarly you can set the data on api calls without finding any id. For that check my next blog.

    Happy Coding :) 

 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: