android databinding using "&&" logical operator

data binding android example
android data binding onclick example
android data binding fragment
android data binding default value
android data binding null check
data binding android example github
android data binding recyclerview
data binding android medium

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;'

&& 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.

Get started, It's recommended to use the latest Android Plugin for Gradle in your project. However, data binding is supported on version 1.5.0 and higher. For  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.

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.

Layouts and binding expressions, When an expression evaluates to a method reference, Data binding wraps the method reference and owner object in a listener, and sets that listener on the target  The Android Support Library includes the Architecture Components, which you can use to design robust, testable, and maintainable apps. You can use the Architecture Components with the Data Binding Library to further simplify the development of your UI. Two-way data binding The Data Binding Library supports two-way data binding.

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;
}

Using data binding in Android - Tutorial, Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. This made Android data binding a pretty laborious, non-intellectual exercise for developers. Data Binding Basics Traditionally, data binding involved inflating XML layout in the activity, finding the element in the layout by using a method such as findViewbyID, assigning the element to a local variable, getting value from the data, and, finally, assigning value to an element property.

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();
}

Data Binding Library, module. Once enabled, Sync the project and you are good to go. We’ve already implemented MVVM using Data Binding and covered LiveData and Data Binding in separate tutorials. Today, we’ll use LiveData with Data Binding in our MVVM Android Application. We’ll see how LiveData makes it easy to update the UI from the ViewModel. Up until now, we’ve used Data Binding to update the View from the ViewModel.

Android working with DataBinding, The Data Binding Library is an Android Jetpack library that allows you to bind UI components in your XML layouts to data sources in your app using a declarative​  The data binding library is a support library, and is available for android platforms from Android 2.1 (API 7) and newer. To use this library in your app, you must download the support repository

Two-Way Data Binding on Android: Observing Your View with XML , Data Binding Library is a support library that enables you to bind UI elements in your layouts to data sources in your app using a declarative  In this blog, I will explain about Loading Images from a URL Using Glide with the help of Data Binding. In the previous blog, I have explained, How to bind View's using data binding in Android.

Data Binding in Android, I've been working with Jetpack Architecture Components lately. For me, Data Binding is an easy component to learn and to use. Even so, I had some issues the  Browse other questions tagged java android data-binding android-button android-databinding or ask your own question. The Overflow Blog The Overflow #19: Jokes on us

Comments
  • I have noticed that I can use ">" and "<" in layout xml files.
  • Don't use domain logic in binding expressions, you have preseters/controllers for this.
  • @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 Imlement a method isMyControlVisible() in your presenter, where you use your domain logic to return true or false back. Bind to this method.
  • I think If you have too many boolean variables this solutions is fine. Otherwise your code would have plenty of ObservableBoolean.set
  • @artkoenig then you would have to maintain those extra booleans everywhere you reference to the layout. Source of bugs.
  • While this is a good solution, it unfortunately does not get called when the variable value changes (notifyPropertyChanged) and the visibility will not be updated.
  • @BerndKampl Use ObservableBoolean isAdult
  • This not going to work, it will only get called once!
  • @BerndKampl how to use it, it is still get called once
  • @CodingTT look at my Answer, you will need a notifyPropertyChanged call somewhere to update it.