Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - DataBinding - How and when the Binding classes will be generated?

DataBinding Guide States

  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 activity_main.xml so the generate class was ActivityMainBinding. 

When will the Binding class , here say ActivityMainBinding, will be generated. I have compile time error. "cannot resolve ActivityMainBinding".

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity); 

Any help is appreciated. Thanks

like image 323
sowmia Avatar asked Jul 12 '15 07:07

sowmia


People also ask

How do you create a binding class?

A binding class is generated for each layout file. 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. The above layout filename is activity_main. xml so the corresponding generated class is ActivityMainBinding.

Which is better Viewbinding and DataBinding?

The main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.


2 Answers

What is your layout name?

The above layout file was activity_main.xml so the generate class was ActivityMainBinding.

What this means is that the generated class name will depend on your layout's name

activity_main.xml -> ActivityMainBinding.java

I think your activity name is "main_activity", so the generated binding class name should be MainActivityBinding not ActivityMainBinding

like image 102
Mao Avatar answered Nov 02 '22 06:11

Mao


When it's not generating the binding class, I restart Android studio. Then the binding class will be generated. Isn't it caused by cache of android studio?

BTW, if you are using android-apt, please check it. Because it will cause binding class not to be generate.

like image 20
Frank Cheng Avatar answered Nov 02 '22 04:11

Frank Cheng