0

So I have a view:

<include
        android:id="@+id/root"
        layout="@layout/layout_awesome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:isVisible="@{viewModel.itemA != null}" />

But in my case, root view visibility should be determined based on two model values in my viewmodel, so I would like to achieve something like this:

<include
        android:id="@+id/root"
        layout="@layout/layout_awesome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:isVisible="@{viewModel.itemA != null && viewModel.itemB == null}" />

However, this does not work. Is there an issue with the syntax? I could not find an example where operators are used.

MaaAn13
  • 264
  • 5
  • 24
  • 54
  • Can you try like this https://stackoverflow.com/a/35756946/5696047 – jayesh gurudayalani Feb 02 '23 at 04:19
  • That's not exactly what I want to achieve, sadly. I want to make sure boolean operator can be used in this case. – MaaAn13 Feb 02 '23 at 05:14
  • 1
    apologies for miscommunication ... What i am trying to say that instead of this ```app:isVisible="@{viewModel.itemA != null && viewModel.itemB == null}"``` use this ```bind:isVisible="@{viewModel.itemA != null && viewModel.itemB == null}"``` and inside ```layout_awesome``` , try this ```android:visibility="@{isVisible ? View.VISIBLE : View.GONE}"``` in your code – jayesh gurudayalani Feb 02 '23 at 05:17
  • 1
    You should replace the && operator with `&&` – Zain Feb 02 '23 at 05:52

1 Answers1

0

You should replace the logical operators like this :

'&' --> '&amp;'

'<' --> '&lt;'

'>' --> '&gt;'

So, the correct xml will be this :

<include
        android:id="@+id/root"
        layout="@layout/layout_awesome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:isVisible="@{viewModel.itemA != null &amp;&amp; viewModel.itemB == null}" />

reference : android databinding using "&&" logical operator