Binding android data using the logical operator "&&"

I am trying to use the & && operator in xml using Android data binding,

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

but I got a compilation error:

Error: execution completed 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 object name should immediately follow the "&" in the object reference.

and a red highlighting error in the android studio "unescaped and or non terminated character".

So how should I fix this?

Edit: found the answer, this character should be escaped:

 '&' --> '&amp;' '<' --> '&lt;' '>' --> '&gt;' 
+83
android android-databinding bindable
May 11 '16 at 3:54
source share
4 answers

&& should display as &amp;&amp; ,

The official data binding guide contains examples of comparison operators that use, for example, these XML objects.

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

edit

The examples of expressions that I mentioned in the answer have disappeared from the English version of the document since this answer was written. They survive in some obsolete non-English versions of documents, such as the Spanish version.

In any case, the original answer remains valid, because the use of XML objects in XML is standard in XML and has nothing to do with Android itself.

+170
May 11 '16 at 4:11
source share

Escaping && in layout layout is a very bad solution. It is better to create a method for the object (view) of the model:

 android:visibility="@{user.adult ? View.VISIBLE : View.GONE}" public boolean isAdult() { return age >= 18; } 
+15
Sep 30 '16 at 10:04 on
source share

HTML Object List

You cannot use & or some other HTML objects in XML. Therefore, you must use the escape character.

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

HTML character objects commonly 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 objects.

+7
Jul 31 '18 at 11:54
source share

The best solution I could solve for this problem is to introduce a new Bindable method.

Before:

item_recyclerview.xml :

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

MyDataModelClass : (which is stored in my view model)

 ... 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 stored in my view model)

 ... 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(); } 
0
Dec 22 '16 at 12:43
source share



All Articles