Skip to content

Data Binding defines a strategy that efficiently connects the UI and the code data. Whereas View Binding connects UI views and the source code. Both this strategies are effective and reduce system processing time.

Notifications You must be signed in to change notification settings

NdaedzoPhoshoko/DataBinding-ViewBinding

Repository files navigation

Data Binding with View Binding

Documentation here

Data Binding https://developer.android.com/topic/libraries/data-binding
View Binding https://developer.android.com/topic/libraries/view-binding#java

Note that this strategy is effective as it reduces the processing time. This style does not advise the use of the old approach of referencing views using the famous findViewById() method.

Approach steps

Step 1 Add the below configuration in the gradle.build module -> android tag

Screenshot (279)

Step 2 Design your UI layout file, assuming takes the name activity_main.xml, include the root element into the layout element, for data binding also include the data element with the variable name and type, the type refers to the class the raw data belongs to (model), see below

pic2

Step 3 Now the system auto-generates a binding class corresponding to the name of your UI layout file. Mine is ActivityMainBinding, so in MainActivity.java, I'll declare the instance of this ActivityMainBinding class. Then we need to connect the inflated layout and the binding utility from this activity

pic4

Step 4 We are done, we just use the binding instance to help us access all the data and views without using the ID or any help of findViewById()

Make sure that you follow me on Linkedin.com, here Ndaedzo Phoshoko (FullStack Developer path). Note Views will not be onbind if you do not give them IDs, so take note of this whenever you perform binding for your data with views.

See my example below

ViewBindingAndDataBinding-ezgif com-video-to-gif-converter

About

Data Binding defines a strategy that efficiently connects the UI and the code data. Whereas View Binding connects UI views and the source code. Both this strategies are effective and reduce system processing time.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages