Imports and includes in data binding

In my earlier posts we have done some basics with data binding like using Recycler view, custom binding adapters/attributes etc. Now we will look into some simpler but much useful things in data binding its imports and includes. Most of us have used another layout in our current layout in android using includes, so how will we bind our data with those includes as well. This problem will be solved in includes in data binding.

Also data binding’s main advantage/ purpose is that we don’t have to play with views in our code or we can say java file but in many cases our view will require more things then only data. This will be included in imports in data binding.

Imports: It allows us easy reference to java classes. Its same like the import in java classes. Lets take an example, we want to show and hide our view based on some data then what will we do?

We will simply import the View class in our layout then will use it for deciding visibility of a view. Just like i did it in the below example:

And its done. You can import any class you want to use its functionality in the xml file.

Includes: If you want to include another layout in your xml layout and want that layout to use binding as well then you can simply bind your object with the include tag as bind:user=”@{user}”. One of the example of the layout is

But there should be a variable named person in address.xml which will bind with your variable in this xml. And the included layouts as binded from within this layout.

. . .

Comments (3)

Add Your Comment

  • Data Binding in android with View ID - Mobikul
    […] <<previous […]
  • Pedro Malta
    You are missing the XML bind namespace on your last code “xmlns:bind=”http://schemas.android.com/apk/res-auto”
    But thanks, helped me a lot!
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close