David Cheung David Cheung - 6 months ago 88
Android Question

android databinding using "&&" logical operator

I am trying to use the and "&&" operator in xml using Android databinding,

android:visibility="@{(bean.currentSpaceId == bean.selectedSpaceId **&&** bean.currentSpaceId > 0)? View.VISIBLE: View.GONE}"


but I got the compilation error:


Error:Execution failed for task ':app:dataBindingProcessLayoutsDevDebug'.
org.xml.sax.SAXParseException; systemId: file:/Users/path/app/build/intermediates/res/merged/dev/debug/layout/fragment_space.xml; lineNumber: 106; columnNumber: 89; The entity name must immediately follow the '&' in the entity reference.


and red highlight error in android studio "unescaped & or non terminated character".

So how should I fix this?

Edit:
found the answer, these character needs to be escaped:

'&' --> '&amp';

'<' --> '&lt';

'>' --> '&gt';

Answer

My guess is that you should render && as &amp;&amp;.

The official data binding guide has examples of comparison operators where these XML entities are used, for example

android:visibility="@{age &lt; 13 ? View.GONE : View.VISIBLE}"