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
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);
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;
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>
Make sure to add below lines in build.gradle file
dataBinding { enabled true }
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.
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>