Data-Binding takeaways

tanya anand
2 min readMar 9, 2021

--

Up to this point, in order to update our UI, we have been pushing data into widgets from our Java or Kotlin code.

This article explores where to use Android’s data binding support to perhaps simplify your Android app development.

Data binding is an opt-in feature, in part because it can slow down the build process. For small projects, you will not notice the overhead of the data binding tools, but that overhead becomes more annoying as projects get larger.

Layout resources with a root <layout> element are processed by the data binding portion of the build system.

That <layout> element then has two children. The first child is an <data> element, and that is where we configure how data binding should proceed when this layout resource gets used. The second child is our ConstraintLayout, representing the root View or ViewGroup for the resource.

Inside of <data>, we can use element <import>.This works like import statements in Java or Kotlin, indicating a particular class that we would like to reference. In the world of data binding, mostly we use <import> for classes where we wish to refer to static methods or fields.

Data binding has its limits. One limit is that it can only access public functions and properties.

Developers that love data binding seem to focus a lot on the “separation of concerns” that data binding helps to enforce. Your Java/Kotlin code can stop thinking about widget details quite so much, with a lot of that code moving to the layout resources.

Detractors point out that:

  • Data binding slows down the build process, as there is more code generation that needs to go on.
  • Data binding can be difficult to debug, as more code is hidden from view, buried in data binding expressions, and generated classes.
  • Data binding adds a bit more bloat to a project, adding three libraries and these generated classes, where the user gains very little from the results.

Google promotes data binding as part of the Jetpack. However, whereas some aspects of Jetpack are nearly unavoidable, data binding is optional. You can use it if you like or skip it if you like.

Points gathered from Book Elements of Android Jetpack by Mark L. Murphy

--

--

tanya anand
tanya anand

No responses yet