Why is data binding preferred over other conventional ways? Data binding is one of the most trending and hottest topics in the android development sector. Data binding library is a support library in android that can be used to bind the UI components in your layouts to data sources in your app using a declarative format rather than programmatically.
It is an android jetpack library.
Usage of data binding
- It is available through android support repository.
To check whether support lib installed or not go to android sdk->sdk tools->support repo->install android support repo.
- Add data binding library to android studio project.
To do that open build.gradle(module-app)->in android element add dataBinding {enabled = true}->sync project.
- Create data class called contact and add parameters with constructor.No need to add getter and setter because this data class will be used with data binding lib.
- Configure a data binding layout.To make any layout as a databinding layout the root view must be <layout></layout>.The immediate child of the layout must be <data></data>.Child of data is <variable> which is used to specify the variable used in this layout.2 properties must be specified in variable name and type.
<variable
name=”contact”
type=”com.example.vibhu.databindingexample.contact” //class which is used for databinding
/>
The next child of layout after data is your actual layout i.e linearlayout or something.
For eg. In textview we can use databinding properties created in class contract . Suppose while setting text for textview we can use them as “@{contract.first}” //first is one of the propties created in class.{} is used for using databinding variables.
- Now in main activity we can create databinding object in any activity whose layout has been configured as databinding layout for example in mainactivity.We can create the object while inflating the layout. Hence in onCreate() we remove the setContentView statement and here we will create our data binding object.What happens is that whenever you compile a databindinglayout it creates a class (for eg here it will be AcitivityMainBinding) in background with the same name as of layout and we can access this created class from mainActivityclass .We do
ActivityMainBinding activitymainbinding=DataBindingUtil.setContentView(this,R.layout.activity_main);
Now we can set data for the databinding object .For this we do
Contact contact=new Contact(“abc”,”efg”);
Activitymainbinding.setContact(contact);
- Everything is done and now you can run.
Why is data binding preferred over conventional method?
1.It is used to directly bind ui components to its data sources from layout itself.
2.To prevent repeated calls to findviewbyid and to improve performance of the app
3.Memory leaks and null pointer are prevented.
- If layout is converted into data binding layout ,all the expensive findViewbyid calls will be prevented.Suppose if in a normal layout a calculator has to be made then all the findviewbyid calls will be made corresponding to each button .Those can be prevented using declarative layout.
- Declarative layout also makes layout more flexible.
6.Stronger readability – depends if you are a new developer then you may find it easy to learn it but if you previously worked on android you will need extra time to learn it.
7.Powerful – the code has more power, you can implement whatever you like in code. Think about it like this, everything you implement using data binding has a code equivalent (it might be longer and more code to write), but the revers is not valid.
8.Testing the UI — with data-binding it’s much more easy to test the UI, but data-binding it’s not enough for that, you also need a good architecture and using the Google suggested architecture will show the actual power of data-binding.
Data Binding in included layouts
1.In included layouts,the parents and the child both must be databindinglayout.
2.We must pass object of contact from parent layout to child layout.For this in includes statement we write
<include
Id=”+@id/content”
Layout=”//the layout to be included”
bind:contact=”{@contact}”
/>
How to handle click events using data binding library
1.Make a class inside main only which will be used to handle the clicking of button.
Name it as ClickHandler .Make a property context and its constructor in it .Also add a method called as SimipleButtonclick in it which takes parameter as View and in this we write whatever we want to do while clicking.
In both parent and child we have to make variable corresponding to it.
In on click of the button in layout we write as “@{clickHandler:: SimipleButtonclick}”
2.Now in the class of those activities we can make clickhandler object.
ClickHandler clickHandler=new ClickHandler(this);
activitymainbinding.content.setClickHandler(clickHandler); //content is the id of included layout
3.Similarly we can do for longclick
For long click we have to create a method in class and this method should have a Boolean return type and not void.Then similar procedure.
How to use clicklistener with parameters
1.Create a simple method which takes parameters as View object and Content object.
2.In xml now in onclick do “@{(v)→onclickhandler.setclickhandledwithparams(v,contact)}”
3.same procedure as conventional method
Recycler View using data binding library
1.Follow the basic steps for recycler view as in normal.Also make the layout for single item using data binding library only.Add data and variable only in single item layout because for this example we dont need any kind of data in mainLayout.
2.Checkout the dataBindingExample from the projects.