I'm trying out the new data binding library. I have a weird issue where binding the visibility
property is not compiling.
This is a simplified version of the xml file:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="header" type="com.example.EmailHeader" /> </data> <RelativeLayout ... > <TextView ... android:text="@{header.senderName ?? header.senderAddress}" android:visibility="@{header.hasAttachment ? View.VISIBLE : View.INVISIBLE}" /> </RelativeLayout> </layout>
I get the follow message when compiling:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Identifiers must have user defined types from the XML file. View is missing it
Everything compiles (and works!) when I remove the android:visiblity
declaration.
I don't see what I'm missing here
The above layout filename is activity_main. xml so the corresponding generated class is ActivityMainBinding . This class holds all the bindings from the layout properties (for example, the user variable) to the layout's views and knows how to assign values for the binding expressions.
Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression. Expressions can be used for many purposes depending on your requirement.
Inside of the data tag you need to also add:
<import type="android.view.View" />
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