Data Binding in Android with Kotlin

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()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store