In this example, we will see how to resolve the error “unresolved reference data binding” error in Kotlin.

An unresolved reference error indicates that the compiler is unable to locate the classes and resources associated with data binding.

Check gradle configuration

To resolve this error make sure that the data binding configuration is enabled in the Gradle configuration.

Open the Android project’s build.gradle file and add the following information in the Android block.

android {
    dataBinding {
        enabled = true

Once the above changes are made sync the project with Gradle to apply the changes.

Check if packages are imported

Make sure that the necessary packages are imported. for example, the following packages may be needed when using data binding.


// PROJECT related databinding

Check the layout file

We must make sure the layout files are properly set up for the data binding.

In the layout XML file, we should have <layout> tag at the root level of the document and inside that rest of the content should be present such as data binding expressions if needed.

<layout xmlns:android="">
    <!-- Rest of the layout file content -->

After all the above modifications just rebuilt the project to trigger data binding code generation.

If still the issue is not resolved, you can see the following example of how to implement the data binding in the right way.

How to use data binding in the right way?

Enable gradle configuration:

In the build.gradle file, add the following change inside the android block.

android {
    dataBinding {
        enabled = true

Create the layout file

In the following activity_main.xml file, we have defined the data binding expression. Inside the <variable> tag, we specified the name & type of the variable. Using this we bind the instance of the User class to the layout.

In the textView, we use the data binding expression android:text="@{}" to bind the name property of the User object to the TextView which makes the TextView change dynamically when the name property of the User class changes.

<!-- activity_main.xml -->

<layout xmlns:android="">

            type="com.example.User" />


            android:text="@{}" />

            android:text="Greet" />


Create the data class

data class User(val name: String)

Accessing views using Data Binding

Add the following inside the Activity or fragment.

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.example.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private val user = User("John Doe") // Create a User instance

    override fun onCreate(savedInstanceState: Bundle?) {

        // Set up data binding
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        binding.user = user // Assign the User object to the binding variable

        // Set up button click listener
        binding.buttonGreet.setOnClickListener {
            val greeting = "Hello, ${binding.user?.name}!"
            binding.textViewGreeting.text = greeting

In the above example, we create an instance of the User class and bind the user object to the variable defined in the layout using binding.user = user. Here we are assuming the data binding package as com.example.databinding. Make sure you modify this as per your project’s data binding package name.

With the data binding, we don’t need to manually find & manipulate the views instead we can access them directly using the data binding object.

Categorized in:

Tagged in: