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.

androidx.databinding.DataBindingUtil

// PROJECT related databinding
com.example.databinding.ActivityMainBinding

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="http://schemas.android.com/apk/res/android">
    <!-- Rest of the layout file content -->
</layout>

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="@{user.name}" 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="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="user"
            type="com.example.User" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textViewGreeting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}" />

        <Button
            android:id="@+id/buttonGreet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Greet" />

    </LinearLayout>
</layout>

Create the data class

data class User(val name: String)

Accessing views using Data Binding

Add the following inside the Activity or fragment.

import androidx.appcompat.app.AppCompatActivity
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?) {
        super.onCreate(savedInstanceState)

        // 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:

,