Android with Databinding in Kotlin (Note)

Android with Databinding in Kotlin (Note)
Post Written By Theethawat Savastham - 11 Oct 2019

Android With Kotlin Note use Databinding

Kotlin the new 1st class language in Android. And today the trend name databinding is so amazing more than using findViewById right? it handle Smoother.

While I study in this for my project, I has taked a note in Markdown and I will show you about my note but I recommend you to go to Android Codelab Kotlin Fundamental Course to learn it. This is only my note taking use sometimes it can help you if you cannot remember all like me

Android with Kotlin Databinding


Active Android-X in your Android Studio

Activatate Databinding

Enable Databinding in build.gradle file that app part inside android{} tag

dataBinding {
    enabled = true

After That Sync your app

Basic Viewbinding

Layout Compatible

if you using linear layout add layout <layout> .... linear layout ..... </layout> over there and move layout name space like xmlns:android="......" to this layout and Build your App


go to MainActivity.kt or other program file

  • Active Binding Variable using lateinit that is predefine without value initial like private lateinit var binding: ActivityMainBinding
  • Set Content View Using Data Binding everything using the attribute of variable that have define in the previous step binding = DataBindingUtil.setContentView(this,R.layout.activity_main)

this tell you that ‘this activity’ and R.layout.activity_main show that layout that you want to bind or showing

Databinding will generate A Specific Variable Name of Attribute like nickname_show to nicknameShow using Camel Case that automatically making by an IDE

Activity Recieving

  • Using Binding Variable in this code using bind in syntax using dot method like

      binding.doneButton.setOnClickListener {

it in Kotlin like this in Java to call compiler that I want to make function on this class

Managing Activity

  • Using Binding Variable and dot syntax like binding.nicknameShow.text = binding.nicknameInput.text
  • Using binding.apply{...} to easy managing code (binding is Binding Variable)
  • Using invalidateAll() to invaluate data (Get new data) when Refresh UI
  • Example on this program

       binding.apply {
          binding.nicknameShow.text = binding.nicknameInput.text
          invalidateAll() /_ to invaluate data (Get new data) when Refresh UI _/
          binding.nicknameInput.visibility = View.GONE
          binding.doneButton.visibility = View.GONE
          binding.nicknameShow.visibility = View.VISIBLE

    Using Attribute As same as using findViewById

  • Using Data Class (in Another Kotlin File) for keep the object type variable like the stuct in C or can use any class instead of data class
  • Link The Layout to that class

          <variable name="myName" type="com.example.aboutme.MyName"/>

The name of the variable you can define but link to your data class or Any structure class.

Source Code - GitHub