Imports and includes in data binding

Updated 15 December 2016

Save

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.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


3 comments

  • Data Binding Part IV: Recycler View with view swicher and data binding - Mobikul
  • Data Binding in android with View ID - Mobikul
  • Pedro Malta
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home