Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 346
Gaspare Avatar asked Oct 14 '25 09:10

Gaspare


2 Answers

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>
like image 110
Martin Zeitler Avatar answered Oct 17 '25 00:10

Martin Zeitler


Just add in the module based Grade file, the following

buildFeatures{
  viewBinding true
}
like image 38
vasantha raj Avatar answered Oct 17 '25 01:10

vasantha raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!