Data Binding In Kotlin Part 1

abhinesh chandra
4 min readOct 7, 2022

--

Learning Agenda

  • what is data binding?
  • data binding layout
  • Advantages of data binding.
  • how to implement simple data binding?

Data Binding

  • As the name suggests, the data binding library allows us to bind UI components of the layout with data sources declaratively rather than programmatically.
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{employee.name}"
/>
  • you can see in the above ex we have used employee.name as a data source to update text.
  • So in this case you don't need to programmatically do findViewById() and store its reference in a variable. then use that variable to update the UI value.
  • Here if we directly update the data source employee.name then UI will automatically get updated.
  • binding.textView.text = "Hello" In view binding, we programmatically update views every time. As the view binding name suggests binding happens between different views of the layout and the binding class.
public TextView textView2 = (TextView) findViewById(R.id.textView2);
textView2.text = "Hello"
  • In findViewById(), we can only update the data of views programmatically.
  • Note — If you are using data binding just to replace findViewById(). It is advisable to use view binding, As it provides similar benefits as data binding, better performance, and simpler and easy-to-understand implementation.

data binding layout

  • you can convert your layout file to a data binding layout by doing a right click at end of encoding and choosing show context actions.
  • Inside actions, you will find the shortcut “convert to data binding layout”. Use this shortcut to convert your layout to a data-binding layout.
  • The shortcut will add a layout tag and a data tag.

Advantages of Data Binding over other UI update methods

  • It helps to avoid making many UI frameworks call in the activity, as the UI gets updated as the associated data source gets updated. This makes the code more straightforward and easier to maintain.
  • It improves app performance.
  • It helps prevents memory leaks and null pointer exceptions. Memory leaks occur when an application allocates memory for an object but fails to release the memory when the object is no longer being used.

Setup Instructions

Configure Gradle file

android {
...
buildFeatures {
dataBinding true
}
}
  • Add the above code to the app-level Gradle file of the project to enable the data binding library in the project.

Declaring a data variable in the layout file.

//activity_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="employee"
type="com.example.databinding.Employee" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
>
....
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
//Employee.kt
data class Employee(val name: String, val profile: String)

data variable employee

  • In the above ex, you can see we have declared a data variable employee of type Employee data class inside the activity_main.xml file.
  • We declare data variable to bind views with data directly.
  • We use this variable to update all the views on data change.
  • To add data variable inside the XML file we need to enclose the parent layout inside the layout tag. Namespace tags are included inside the outermost layout.
  • Then just below the layout tag, we add <data> tag to include the data variable.

data class Employee

  • We have also created a data class Employee. this class will hold data and will be used to update views with different data requirements.

Note

  • Adding layout variable to views ex is already given above, so didn't cover it here.

Binding Activity

//MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
val empObj = Employee("abhinesh chandra","android developer")
binding.employee = empObj
}
}
  • you can see from the above ex we are inflating the layout and creating the binding instance using DataBindingUtil. We use DataBindingUtil when the binding class is not generated.
  • data binding library generates binding class, used DataBindingUtil here for example sake.

Another Approach

class MainActivity : AppCompatActivity() {
//lateinit var binding: ActivityMainBinding
private var _binding: ActivityMainBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val empObj = Employee("abhinesh chandra","android developer")
binding.employee = empObj
}
}
  • data binding generates a binding class which is used to access layouts variables and views.
  • view binding generates a binding class through which you can access only views.
  • you can use both view binding and data binding libraries inside the project. use view binding for layout with simple use cases and data binding for layout with complex use cases.
  • I have used the inbuilt method inflate of binding class to inflate the layout and create a data binding instance.

Updating views using data variable

val empObj = Employee("abhinesh chandra","android developer")
binding.employee = empObj
  • here you can see we have created an object empObj of Employee data class to store data.
  • Then we are assigning empObj to the employee data variable as it is of Employee data class type.
  • employee data variable will update all the views using its properties automatically.
  • you can see how less code is required to update the views in case of data binding. In the case of view binding, we need to update the data of all views separately.

Output

  • here I have used two simple text views to display data using data binding.

--

--