4

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Gaspare
  • 53
  • 1
  • 1
  • 5

4 Answers4

2

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>
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • It is new to me. The old app has not a data tag in the XML layout file. I have never needed to use it. I declare variables and imports in the .java file. XML file contains all graphics issues with IDs to link them with Java object. – Gaspare Sep 15 '18 at 16:46
  • I spent a couple hours looking for a solution to this, and for some reason this was the only one that worked. The `` tag has to be specified or the class won't get generated for me. – nonbirithm Jan 01 '21 at 10:36
  • It may even be required to add an empty `` node, when it won't generate the bindings. – Martin Zeitler Jan 01 '21 at 14:10
2

Just add in the module based Grade file, the following

buildFeatures{
  viewBinding true
}
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
0

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

buildFeatures{ viewBinding true;}
  • He already enabled dataBinding in his app – Alex Rmcf Mar 31 '22 at 10:59
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 31 '22 at 11:00
0

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

class=".databinding.ActivityMainBinding"