New In Android: View Binding. The Difference From DataBinding Library.

Anastasia Finogenova
ProAndroidDev
Published in
4 min readMay 16, 2019

--

No, they are not the same, even if the naming sounds really similar. ViewBindings were introduced at the What’s new in Android talk at Google IO/19. It is a new API and it has a similar concept as DataBinding so let’s figure out the difference and what we can leverage from it.

What is view binding?

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. You can read more here. To enable DataBinding you need to enable the property in Gradle file (starting AndroidX no need to import the library).

ViewBinding serves the same purpose, it is enabled by enabling the property in Gradle. No need to include additional imports.

Why using bindings and not findViewById?

So what difference does enabling ViewBinding/DataBinding make VS just calling findViewById?

  1. Elegance.

findViewById lacks elegance. It is not convenient initializing every view by id and declaring it as separate variables. It leads to a bunch of boilerplate code which clutters Fragment or Activity. ViewBinding/DataBinding can provide a nice way of directly calling views by id with just initializing one variable — the binding class. Check out the difference on the snippets below. The difference might now be that drastic with 2 variables but having 10–15 of them or more can bring a significant boilerplate.

2. Type safety.

Using findViewById is not type safe unlike ViewBinding/DataBinding. Type safety is really important in having robust and ClassCastException free code, so here findViewById is not the…

--

--