1Step 1 : Enable dataninding in build.gradle(app)
2
3android{
4...
5 buildFeatures{
6 dataBinding true
7 }
8}
9
10or
11
12android {
13...
14 dataBinding {
15 enabled true
16 }
17}
18
19Step 2 : Add kapt dependency and apply kapt plugin
20dependencies {
21 ...
22 kapt "com.android.databinding:compiler:3.1.4" (use the version suitable
23 for you)
24}
25
26and
27
28plugins {
29 ...
30 id 'kotlin-kapt'
31}
32
33or
34
35apply plugin: 'kotlin-kapt'
36
37step 3 : Convert the fragment or activity to databinding form by adding
38<layout> tag and move the namespaces to the <layout> tag as following
39
40<layout xmlns:android="http://schemas.android.com/apk/res/android"
41 xmlns:app="http://schemas.android.com/apk/res-auto"
42 xmlns:tools="http://schemas.android.com/tools">
43
44 <data>
45
46 </data>
47 ...
48</layout>
49
50step 4 : remove setContentView(R.layout.activity) and add the following line in
51your fragment or activity class
52
53
54val binding:ActivityMainBinding = DataBindingUtil
55 .setContentView(this,R.layout.activity)
56
57
58
59
60