How awesome is life now that we have got you… Databinding!

Diego Gonçalves Santos
2 min readJun 5, 2017

--

Well yeah, as you can see, today I am gonna talk about a new Android feature that I really loved! Databinding made life easier when you have to add data from a model to your screen. No more “findviewbyid” or even “setText” needed anymore! I will show you how!

To get started you gotta enable Databinding​ in your project by simply adding the code Bellow to your build.gradle file in the app module.

After that you gotta change your layout resource to have as the most top element a <layout> tag as you can see here:

After, you have to declare a variable with the type of your model like this:

With the variable created, it becomes really easy to bind data, just add the call for the model property inside @{}.

To be done you have left one last step. In your activity create your Databinding variable. The type will always have the name of your layout, followed by “Binding” (this class is generated “automagically” after you put your layout inside the layout tag). Now instantiate it replacing the setResource method on your onCreate.

And set your model variable to it like I did below.

If everything goes well, you will be able to see the data being shown in your screen.

Well, that’s it! Hope you devs enjoy it! That’s a pretty handy tool that makes your code way prettier and concise. If you want more information about it you can check the offical documentation at Google Developers page https://developer.android.com/topic/libraries/data-binding/index.html See ya!

--

--

Diego Gonçalves Santos

Engenheiro de Software @Dextra_digital, escritor, palestrante, entusiasta de tecnologia e esportes.