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
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With