63

While using data binding , I am not able to get class MainActivityBinding as per Data Binding Guide

My layout name is activity_main.xml. I am also see Android - DataBinding - How and when the Binding classes will be generated? but it can't help me.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
  • What is your activity name?? – M D Mar 09 '16 at 05:18
  • @MD My actvity name is : `MainActivity.java` – pRaNaY Mar 09 '16 at 05:22
  • 2
    Clean and Built project this will generate DataBinding class again as @RRR said – M D Mar 09 '16 at 05:24
  • Already tried this things but it can't solve my problem! – pRaNaY Mar 09 '16 at 05:25
  • Anyone facing the initial databinding setup try this http://code2concept.blogspot.in/2016/07/android-data-binding-part-1.html – Nitesh Tiwari Jul 30 '16 at 09:34
  • 2
    I noticed that if the xml layout file starts directly from a constraint layout it won't work. Wrap everything in , move xmlns:android="...", xmlns:app="...", and xmlns:tools="..." to it, and then clean and rebuild. The binding object should appear. Also as the others said, make sure your naming is correct. – himura Aug 19 '17 at 15:49
  • 1
    Possible duplicate of [Data Binding class not generated](https://stackoverflow.com/questions/39483094/data-binding-class-not-generated) – Khemraj Sharma Oct 25 '18 at 07:15
  • http://mobologicplus.com/android-custom-data-binding-jetpack-component-directly-with-xml-view/ please check this tutorial – Sunil Kumar Jun 19 '19 at 05:32

29 Answers29

108

DataBinding class will be generated based on your xml file name. It is clearly mentioned in doc you are following.

By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was main_activity.xml so the generate class was MainActivityBinding

If your xml name is activity_main.xml than DataBinding class name will be ActivityMainBinding.

If your xml name is main_activity.xml than DataBinding class name will be MainActivityBinding.

Dont forget to clean and build project once

you can follow this tutorial for more about DataBinding

Rocky
  • 268
  • 2
  • 3
  • 13
  • I am make sure with layout name as I already added in my question. – pRaNaY Mar 09 '16 at 05:30
  • yes but in question you have mentioned `activity_main.xml` and using `MainActivityBinding` , it should be `ActivityMainBinding` – Rocky Mar 09 '16 at 05:51
  • Make sure you rebuild the project after adding xml components, so that this class is generated. – Ajith M A Jul 11 '16 at 05:20
  • 2
    If you struggle with unresolve symbol, try wrap you layout in `` tag. – Nexen Sep 30 '17 at 02:09
  • 1
    Whenever you work with binding follow steps 1: first do dataBinding {enabled = true } in gradle ..sync it ..2: use tag if you have viewmodel then apply it in in your layout tag 3: then go to your activity and see ActivityMainBinding there .. if not again rebuild it and see it must be there ..Thanks – Vishal May 11 '18 at 10:11
  • Thanks a lot. Was suffering from last hour and trying to type `MyActivityBinding` is of `ActivityMyBinding`. Thanks again. – Pooja Jan 07 '21 at 16:50
27

TRY Renaming the xml file to another name and check if binding works once it works rename it back to the one that was used.

That helped for Android Studio 3.1

Ismail Iqbal
  • 2,774
  • 1
  • 25
  • 46
20

Thanks to all for your answer.I found solution with ContentMainBinding class name for data binding. Lets me explain.

NOTE: While using layout with <include ... here is <include layout="@layout/content_main" having Data Binding functionality, the class name related to include layout name. Here is the ContentMainBinding

My layout file are as below:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.databindingdemo.app.MainActivity">
    ...
    <include layout="@layout/content_main" />
    ...
    </android.support.design.widget.CoordinatorLayout>

And content_main.xml is layout where I added my Data Binding layout code.

So instead of using MainActivityBinding it can resolved with ContentMainBinding

The code which work for me is below:

//Code for data binding
    ContentMainBinding contentMainBinding = DataBindingUtil.setContentView(this, R.layout.content_main);
    user = new User("Pranay", "Patel", "demoemail@gmail.com", "9999999999");
    contentMainBinding.setUser(user);

DONE.

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
  • 1
    You have fooled yourself, it will set your activity layout content_main.xml not activity_main.xml, What you have found is the default way to attach activity to layout. – Khemraj Sharma Apr 27 '18 at 08:33
  • **Don't follow this answer**, It will set layout `content_main`, If you want to inflate `activity_main.xml` then wrap it with ` – Khemraj Sharma Aug 01 '18 at 18:01
  • Thanks @Khemraj for suggestion. It is an old answer. It may have an error. Can you please update answer if you can? – pRaNaY Aug 02 '18 at 03:53
17

Make sure your activity_main.xml file is enclosed with layout tags like so:

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

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.constraint.ConstraintLayout>
</layout>
Andrew Lee
  • 446
  • 4
  • 7
9

If DataBinding class name is correct and you used to clean and rebuild project but it still show error.
Then you try to restart AndroidStudio

Linh
  • 57,942
  • 23
  • 262
  • 279
8

I've cleaned the project , Rebuild was done...but of no use. Then invalidated caches and restarted the project that too didn't helped me.

Then I changed my xml file name - that worked fantastically fine.

So, I would like to share you one thing, please change your xml file name.

For eg: If your xml file is: activity_main.xml and you can't get ActivityMainBinding in its Java class..........then change xml name to main_activity.xml and then use MainActivityBinding in its java class as 'private MainActivityBinding binding;'

This will do most probably.

Musthafa
  • 251
  • 5
  • 4
7

In such cases, if neither rebuild nor invalidate caches work, then there must be an error in one of your xml files where you may have used @{xyz.abc} and there must be a problem with either xyz or abc in the xml.

Rohan Taneja
  • 9,687
  • 3
  • 36
  • 48
7

I had the same problem after changing the package name of my source, after I tried a lot of things (including mentioned here), I solved the problem by importing the databinding class manually:

import com.domain.my.databinding.MyActivityBinding;
hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
6

In my case DELETING the the app build folder and then rebuilding the project solved my problem.

Even following steps did not work.

          1) Rebuild the project after adding the dataBinding  in gradle.

          2) Rebuild after adding layout tag in xml.

          3) cleaning the project

          4) rebuilding the project

          5) Restarted the Android Studio

After enabling the dataBinding in app gradle file please rebuild. Then we can find Binding class generated.

          If the layout name is fragment_home, The Binding class name will be FragmentHomeBinding.
Thriveni
  • 742
  • 9
  • 11
  • I also tried to change bindings generation type from AS built-in to compiler and vice versa with restarting AS. Nothing happens. Yesterday everything worked fine – IC_ Sep 02 '18 at 01:39
  • I waste one hour to solve this, then I tried your solution I change name of xml still it doesn't work then I rebuild, clean restart and it works. thanks – Rucha Bhatt Joshi Feb 02 '19 at 12:31
5

Make sure to add below lines in build.gradle file

dataBinding { enabled true }

Revanth Kovuri
  • 161
  • 1
  • 11
4

Check that you activity/fragment's xml and class have consistent names; for example, if you have TimePickerFragment than xml file name should be time_picker_fragment.

It occurred to me that I had the layout named fragment_time_picker; after I changed it the binding was generated.

AAryz
  • 140
  • 7
4

Binding class will be generated based on your layout file name. By default, the name of the class is based on the name of the layout file, converting it to Pascal case and adding the Binding suffix to it

for example, If your xml name is activity_main.xml then DataBinding class name will be ActivityMainBinding

then import the package:

import com.companyname.applicationname.databinding.ActivityMainBinding;

then you can use it

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User("Test", "User");
binding.setUser(user);
Alen Lee
  • 2,479
  • 2
  • 21
  • 30
4
  1. confirm add below code in android {} in build.gradle (:app)
    buildFeatures {
       viewBinding true
    }
  1. Binding's name is the corresponding .xml file name + "Binding", to check the .xml file, hold command and click the Activity name, then you jump to that .xml file OR you may get a dropdown list. No matter which, you can get the Binding. enter image description here

3. if still doesn't work then restart Android Studio

CN_Cabbage
  • 405
  • 3
  • 12
2

Cant comment so i'll just add this in answer. I believe activity_main.xml will create ActivityMainBinding rather than MainActivityBinding as you mentioned. in some cases where studio can't find the binding class then just invalidate the caches and rebuild the project.

Ankit
  • 429
  • 3
  • 16
2

By default, a Binding class is generated based on the name of the layout file, starting it with upper-case, removing underscores ( _ ) and capitalizing the following letter and then suffixing “Binding”.

This class will be placed in a databinding package under the module package.

For example, the layout file contact_item.xml will generate ContactItemBinding.

If the module package is com.example.my.app, then it will be placed in com.example.my.app.databinding.

Binding classes may be renamed or placed in different packages by adjusting the class attribute of the data element. For example:

<data class="ContactItem">
    ...
</data>

This generates the binding class as ContactItem in the databinding package in the module package. If the class should be generated in a different package within the module package, it may be prefixed with “.”:

<data class=".ContactItem">
    ...
</data>

In this case, ContactItem is generated in the module package directly. Any package may be used if the full package is provided:

<data class="com.example.ContactItem">
    ...
</data>
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
2

Restart Android studio or try to rename the XML file to another name and check if binding works once it works.

Brahem Mohamed
  • 342
  • 2
  • 5
1

In my case: Solve problem without renaming XML file.

I checked all case and I did everything like Invalidate Caches/Restart Android Studio, clean Project, Rebuild Project but But the error is not resolved.

Solution - Just I change some code and revert that code again in activity XML file or change code in XML file.

Note - This is very late answer but may be help others who don't want change XML file name. :)

1

I was facing the same issue,

If your XML name is activity_main.xml than DataBinding class name will be ActivityMainBinding

If it is correct check if you add below code in your xml file,

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable
        name="xyz"
        type="com.example.yourapp.classname" />
   />

</data>

if either method is not the issue, try

  • clean project
  • Rebuild project
Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
1

In my own experience, whenever you are including a layout, make sure the root view group has a layout id as shown below. Note the child_layout id in the Child Layout file.

ParentLayout

        <include
            android:id="@+id/child_layout"
            layout="@layout/child_layout"/>

ChildLayout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/child_layout"
    android:layout_height="match_parent"
    android:background="#FFFFFe">
Andrew Chelix
  • 1,012
  • 11
  • 16
1

This line inside module/build.gradle solved the issue for me:

buildFeatures {
    viewBinding true
}
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
0

I've tried the following solutions which didn't work for me: 1) Invalidate cache and restart. 2) Restart Android Studio. 3) Rebuild project.

What DID fix the problem is: 1. Removing the "dataBinding" tag in the module gradle.build 2. Sync project 3 Return the "databinding" tag 4. Sync project again.

Nativ
  • 3,092
  • 6
  • 38
  • 69
0

Try restarting Android Studio, or manually searching for the ActivityMainBinding class and adding your import.

Put your data tag in your last included xml. Here is an example:

MainActivity.java
public class MainActivity extends AppCompatActivity {

    public Item item;
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        item = new Item();
        item.setChecked(true);
        item.setName("a");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.included.secondIncluded.setModel(item);


Item.java
public class Item extends BaseObservable {
    private String name;
    private Boolean checked;
    @Bindable
    public String getName() {
        return this.name;
    }
    @Bindable
    public Boolean getChecked() {
        return this.checked;
    }
    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }
    public void setChecked(Boolean checked) {
        this.checked = checked;
        notifyPropertyChanged(BR.checked);
    }
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="abc"
            android:textSize="20sp" />
        <include
            android:id="@+id/included"
            layout="@layout/included_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</layout>


included_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="123"
            android:textSize="20sp" />
        <include
            android:id="@+id/second_included"
            layout="@layout/second_included_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</layout>

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

    <data>
        <variable
            name="model"
            type="com.example.simone.twowaydatabinding.Item" />
    </data>

    <LinearLayout
        android:id="@+id/linear_layout_included"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xyz"
            android:textSize="20sp" />
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={model.name}"
            android:textSize="20sp" />
        <Switch
            android:id="@+id/switch_test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="@={model.checked}" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="change"
            android:onClick="button_onClick"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/world"/>
    </LinearLayout>
</layout>       
live-love
  • 48,840
  • 22
  • 240
  • 204
0

I recently installed android studio 3.5.3 and this issue rised so what i did is.

1) Clean Project
2) Update Gradle (as notification was coming) 
3) Rebuild project

hope this helps.

UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278
0

try to review your xml, if has a value which was setted wrong. This is probably something about wrong databinding settings

Aury0n
  • 217
  • 3
  • 14
0

Please put this code in build.gradle(app level), if not declared.

android {
    dataBinding.enabled true
    viewBinding {
        enabled = true
    }
}

// Android Studio 4.0

android {
    dataBinding.enabled true
    buildFeatures {
        viewBinding = true
    }
}
apurv thakkar
  • 8,608
  • 3
  • 14
  • 19
0

I also faced same issue. But after updating android to androidx solved like

try to change android.databinding.... to androidx.databinding...

0

Enclosed with layout tags

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

   <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.constraint.ConstraintLayout>
</layout>
Raviraj
  • 906
  • 9
  • 14
0

Hi the accepted answer is outdated, the new answer is include this in the build.gradle(:app)

inside the android block

buildFeatures { viewBinding true }

works 100%

Zaid Zakir
  • 543
  • 3
  • 7
0

FYI, I am using Android Studio Bumblebee | 2021.1.1

I tried all basic steps such as

  • Clean & build project
  • Invalidate caches and restart

All above not working. Then I found it by myself after going through the Gradle file.

Add below code into module/build.gradle file inside android tag.

kotlinOptions {
    jvmTarget = '1.8'
}
Parth Patel
  • 859
  • 2
  • 11
  • 28