Gaspare
Gaspare

Reputation: 53

databinding.ActivityMainBinding unable to be imported in Android project

I have a working Android Studio project, also installed via USB on my smartphone, it's a simply basic calculator. Now I have to rewrite the code for a lesson I teach, but Android Studio denies this import

import [com.username.projectname].databinding.ActivityMainBinding;

The app uses dataBinding I have enabled in Module:app and resync everything.

The old project works, the new one not, with the same code, cause of this import is refused.

Upvotes: 4

Views: 14594

Answers (5)

Ahmad Jihad
Ahmad Jihad

Reputation: 1

Most of the problem is whenever you add

buildFeatures {viewBinding = true} in build.gradle.kts(module: app) we forget to hit the (sync now) button Hit that button up there as it appears in this image

then go back to your Java code and add ActivityMainBinding binding(whatever you want). and it should work.

Upvotes: 0

Lalaa Rukh
Lalaa Rukh

Reputation: 1

Sir you just need to add the following line in your layout in file activitymain.xml

class=".databinding.ActivityMainBinding"

Upvotes: 0

Senura Perera
Senura Perera

Reputation: 1

add this code in the build Gradle (:app) file :

buildFeatures{ viewBinding true;}

Upvotes: 0

vasantha raj
vasantha raj

Reputation: 36

Just add in the module based Grade file, the following

buildFeatures{
  viewBinding true
}

Upvotes: 2

Martin Zeitler
Martin Zeitler

Reputation: 76809

The XML needs a <layout> wrap alike this ...so that the desired class will be generated:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data class=".databinding.ActivityMainBinding">
        <!-- add variables & imports into here -->
    </data>

    <!-- and add the RelativeLayout into here -->

</layout>

Upvotes: 2

Related Questions