Data Binding in Android with Kotlin

Alisha Bindal
3 min readJan 18, 2021

--

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. It is potentially really powerful and complex, but when used effectively it can really cut down on presentation boilerplate.

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

Data Binding — The Idea

  • The idea about data binding is to create an object that connects/maps/binds two pieces of distant information together at compile time, so that you don’t have to look for it at runtime.
  • The object that surfaces these bindings to you is called the Binding object. It is created by the compiler, and while understanding how it works under the hood is interesting, it is not necessary to know for basic uses of data binding.

Data Binding vs findViewById

  • findViewById is a costly operation because it traverses the view hierarchy every time it is called.
  • With data binding enabled, the compiler creates references to all views in a <layout> that have an id, and gathers them in a Binding object.
  • In your code, you create an instance of the binding object, and then reference views through the binding object with no extra overhead.

Implementing Data Binding Views and Data

  • Updation of data and the data displayed in views is unmanageable and is a source of errors, thus keeping the data in the view also violates separation of data and presentation. Therefore Data Binding solves both of these problems. You keep data in a data class by adding a <data> block to the <layout> to identify the data as variables to use with the views. (Views reference the variables)
  • The compiler then generates a binding object to bind the views and the data.
  • We reference and update the data through the binding object, which updates the data which is displayed in the view.

Steps to be followed

  • First of all 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
}
}
  • In our xml we will wrap all views into a <layout> tag, and move the namespace declarations into the the <layout> tag.
  • In the Activity class, for the initialization we will follow the following steps:
<!--create a binding object -->
private
lateinit var binding: ActivityMainBinding
<!--in onCreate, use DataBindingUtil to set the content view -->binding = DataBindingUtil.setContentView(this, R.layout.activity_home)<!--Use the binding object to replace all calls to findViewById -->binding.btnAction.setOnClickListener(this)
  • Create a data class MyName for the firstName and lastName.
data class MyName(var firstName: String = "", var lastName: String = "")
  • Add a <data> block to xml. The data block goes inside the layout tag but before the view tags. Inside the data block, add a variable for the MyName class.
<data>
<!-- Declare a variable by specifying a name and a data type. -->
<!-- Use fully qualified name for the type. -->
<variable
name="myName"
type="com.example.sampleApp.MyName" />
</data>
  • In the xml tvFirstName, tvLastName, replace the references to string text resources with references to the variables
<!-- for first name -->
android:text="@={myName.firstName}"
<!-- for last name -->
android:text="@={myName.lastName}"
  • In the activity, create an instance of MyName
<!-- Instance of MyName data class -->
private val myName: MyName = MyName("abcd","efgh")
<!-- in onCreate(), set binding.myName to it -->binding.myName = myName<!-- Invalidate all binding expressions and request a new rebind to refresh UI -->
invalidateAll()

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Alisha Bindal
Alisha Bindal

No responses yet

Write a response