Answers for "can we use databinding and binding in android studio"

2

databinding in android

Step 1 : Enable dataninding in build.gradle(app)

android{
...
  buildFeatures{
        dataBinding true
    }
}

or

android {
...
    dataBinding {
       enabled true
    }
}

Step 2 : Add kapt dependency and apply kapt plugin
dependencies {
	...
	kapt "com.android.databinding:compiler:3.1.4" (use the version suitable 
    for you)
}

and

plugins {
	...
    id 'kotlin-kapt'
}

or

apply plugin: 'kotlin-kapt'

step 3 : Convert the fragment or activity to databinding form by adding 
<layout> tag and move the namespaces to the <layout> tag as following

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    
     <data>

    </data>
    ...
</layout>

step 4 : remove setContentView(R.layout.activity) and add the following line in
your fragment or activity class 


val binding:ActivityMainBinding = DataBindingUtil
			.setContentView(this,R.layout.activity)
Posted by: Guest on August-11-2021

Code answers related to "can we use databinding and binding in android studio"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language