155

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:

'&' --> '&'

'<' --> '&lt;'

'>' --> '&gt;'
chub
  • 5,131
  • 4
  • 35
  • 59

4 Answers4

268

&& should be rendered 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}"

Edit

The example expressions I mentioned in the answer disappeared from the English version of the docs since this answer was written. They do survive in some outdated non-English versions of the docs such as the Spanish version.

Either way, the original answer is still valid, because the use of XML entities in XML is standard in XML and has nothing to do with Android itself.

  • 14
    Don't use domain logic in binding expressions, you have preseters/controllers for this. – artkoenig Jun 09 '17 at 20:37
  • 3
    @ArtjomKönig what about if you have to hide/show some controls in the UI? I use data binding library to NOT write UI code – Krusty Jan 18 '18 at 15:07
  • 3
    @Krusty Imlement a method isMyControlVisible() in your presenter, where you use your domain logic to return true or false back. Bind to this method. – artkoenig Jan 18 '18 at 15:23
  • 1
    I think If you have too many boolean variables this solutions is fine. Otherwise your code would have plenty of ObservableBoolean.set – Lester Feb 26 '18 at 09:58
  • @artkoenig then you would have to maintain those extra booleans everywhere you reference to the layout. Source of bugs. – Ali Kazi Jan 18 '19 at 01:29
49

List of HTML entities

You can not use & or some other HTML entity in XML. So you have to use escaping character.

android:text="@{(1==1 &amp;&amp; 2>0) ? `true` : `false`}"

HTML Character entities often used in Android:

+--------+----------------------------+--+--+--+
| Symbol | Equivalent HTML Entity     |  |  |  |
+--------+----------------------------+--+--+--+
| >      | &gt;                       |  |  |  |
+--------+----------------------------+--+--+--+
| <      | &lt;                       |  |  |  |
+--------+----------------------------+--+--+--+
| "      | &quot;, &ldquo; or &rdquo; |  |  |  |
+--------+----------------------------+--+--+--+
| '      | &apos;, &lsquo; or &rsquo; |  |  |  |
+--------+----------------------------+--+--+--+
| }      | &#125;                     |  |  |  |
+--------+----------------------------+--+--+--+
| &      | &amp;                      |  |  |  |
+--------+----------------------------+--+--+--+
| space  | &#160;                     |  |  |  |
+--------+----------------------------+--+--+--+

Here is a complete list of HTML entities.

Community
  • 1
  • 1
Khemraj Sharma
  • 46,529
  • 18
  • 168
  • 182
19

Escaping && in the layout mark-up is a very poor solution. It is better to create a method on the (view)model object:

android:visibility="@{user.adult ? View.VISIBLE : View.GONE}"

public boolean isAdult() {
    return age >= 18;
}
Ollie C
  • 27,184
  • 33
  • 125
  • 211
4

The best solution that I could come up with for this problem was introducing a new Bindable method.

Before:

item_recyclerview.xml:

<EditText
...
android:enabled="@{myViewModel.myDataModelClass.lastAddedItem &amp;&amp; !myViewModel.myDataModelClass.editTextDisabled}"
/>

MyDataModelClass: (which is being held in my viewmodel)

...
private boolean lastAddedItem;
private boolean editTextDisabled;
...
@Bindable
public boolean isLastAddedItem() {
    return lastAddedItem;
}
public void setLastAddedItem(boolean lastAddedItem) {
    this.lastAddeditem = lastAddedItem;
    notifyPropertyChanged(BR.lastAddedItem);
}
@Bindable
public boolean isEditTextDisabled() {
    return editTextDisabled;
}
public void setEditTextDisabled(boolean editTextDisabled) {
    this.editTextDisabled = editTextDisabled;
    notifyPropertyChanged(BR.editTextDisabled);
}

After:

item_recyclerview.xml:

<EditText
...
android:enabled="@{myViewModel.myDataModelClass.enableEditing}"
/>

MyDataModelClass: (which is being held in my viewmodel)

...
private boolean lastAddedItem;
private boolean editTextDisabled;
...
@Bindable
public boolean isLastAddedItem() {
    return lastAddedItem;
}
public void setLastAddedItem(boolean lastAddedItem) {
    this.lastAddeditem = lastAddedItem;
    notifyPropertyChanged(BR.lastAddedItem);
    notifyPropertyChanged(BR.isEnableEditing);
}
@Bindable
public boolean isEditTextDisabled() {
    return editTextDisabled;
}
public void setEditTextDisabled(boolean editTextDisabled) {
    this.editTextDisabled = editTextDisabled;
    notifyPropertyChanged(BR.editTextDisabled);
    notifyPropertyChanged(BR.isEnableEditing);
}
@Bindable
public boolean isEnableEditing() {
    return isLastAddedItem() && !isEditTextDisabled();
}
Bernd Kampl
  • 936
  • 1
  • 10
  • 23
  • That code is using BaseObservable classes, BR is the generated binding class. See this post for a more detailed explanation: https://medium.com/@jencisov/androids-data-binding-s-baseobservable-class-and-bindable-annotation-in-kotlin-1a5c6682a3c1 – Bernd Kampl Mar 02 '20 at 12:44