Android’s Data Binding with Kotlin

I assume you are (or you are trying to be) an Android developer. Then I ask: have you already tried to use Data Binding?

It’s a library which allows you to bind the data of your models directly to the xml views in a very flexible way.

My first experience with this was a year ago, trying to set more than 10 different fonts to the Android App I was developing at that moment. For me that was the discovery of a powerful tool. By then, another powerful tool I was flirting with during that period of time was the programming language called Kotlin.

Now in this post I want to show you the approach I used to mix the power of Data Binding with Kotlin with a simple example.

First of all, after having a created Android project in Android Studio, we need to add the Data Binding dependency and the ones of Kotlin to the build.gradle file of our app.

apply plugin: 'kotlin-android'                       
apply plugin: 'kotlin-kapt'
android {
dataBinding {
enabled = true
dependencies {
// notice that the compiler version must be the same than our gradle version
kapt ''

That’s all the configuration we need to start using Data Binding with Kotlin. Thank you very much for reading me… Now lets continue with the fun starting to see the code.

First we need to create a model. In this case a basic one like User.kt

data class User(val name: String, val age: Int)

In our activity_main.xml we can do something like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android=""
<!-- Inside the layout tag it is possible to set the data tag in order to set one or many variables. For this example we are having an User property-->







Remember to always set your usual xml view inside the <layout> tag and attach all the “xmlns:” properties to it. Otherwise it will throw a compilation error, since the generated files will have duplicated properties.

And here comes the Binding:

package com.kuma.sample

import android.databinding.DataBindingUtil
import android.os.Bundle
import com.kuma.kotlinsteps.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)

val user = User("Kuma", 23)
binding.setVariable(BR.user, user)


In that code snippet there are somethings to be noticed:
 *Now exists a class called ActivityMainBinding, which is autogenerated from the activity_main.xml and this contains all the references to use the views that the xml contains.
 *The way of making an instance of ActivityMainBinding is a little variation of the way of setting the xml layout to an activity.
 *There is also a new BR class which is some kind of secondary R class used to store the variables declared on the data tag of the xml.
 *After setting the variable to the binding object, it is necessary to call the executePendingBindings() in order to set the user variable attributes to the marked views.

After compiling this you’ll be able to see that the Data has been set to your view without the necessity of writing any textView.text =

This was my simple approach to use DataBinding inside a Kotlin project. I hope this is being useful to you.