Data Binding Library in android

Hello Friends, Here We are going to discuss regarding Data Binding Library in android. As the name suggests it will help you to bind data with UI components inside your layout file.

We android developers usually bind data with UI components like image view, text view, etc write code inside Activity or Fragment java file. But data binding comes with a new syntax in which we can bind data from layout file instead of java file. Let see how it works.

First you need to enable Data binding in your application with module level gradle file as given below

Next inside layout file of activity we can wrap designing code with <latyout> tag as given below and also declare variable as per requirements

Then inside MainActivity.java we can write following code to bind message with variable

Here DataBindingUtil class will help to set layout and also return binding class instance then using binding class instance we can set value with message variable using setMessage() method

Next if you want to set data of model class instance then here is another code you can follow

User.java

Here inside User class @BindingAdapter annotation we use to create custom attribute to set image inside image view

For Glide library you can find the dependency here

activity_main.xml

MainActivity.java

So friends Data Binding makes easy to bind data with UI components without touching the components we only need to set value with variable we declare inside layout file.

I hope you understand everything if still any query check the you tube video I have embedded or you can also comment in comment box.

Related Posts

Leave a Reply