45

With databinding we now often see codes in layout files like this:

<Variable name="displayIt" type="Boolean"/>

and then later:

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

(of course android.view.View must first be imported for View.VISIBLE and View.GONE to have any meaning)

This makes controlling the view much easier. It also tells me that conditional statements are allowed in XML Layout, but it looks like my google-fu is weak, I tried and couldn't find the syntax for this. What if I want to use literals? Something like:

android:text="{@isValid ? "valid" : "invalid"}"

(yes I know that's a stupid way of doing it, I am just talking about the syntax here). Or what about resource ID's? Maybe like:

android:color="@{isValid ? R.color.green : R.color.red}"

Can it be done? What's the proper syntax?

htwu
  • 467
  • 1
  • 4
  • 9

5 Answers5

94

The correct syntax for calling a data-bind statement looks like "@{<some expression>}", and so a ternary conditional would be

"@{bool ? ifTrue : ifFalse}"

Where those two values would be the (unquoted) values of what you would normally place into the XML without data binding.

For example

android:color="@{isValid ? @color/green : @color/red}"

Or, you can import a class that has a static field that you need, for example

<data>
    <import type="android.view.View"/>
</data>

And

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

Both of which are shown in the data binding documentation

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
24

simple syntax

android:text="@{user.gender ?? `male`}"

is equivalent to

android:text="@{user.gender != null ? user.gender : `male`}"

From Android Documentation, you have many available expressions

Mathematical + - / * %
String concatenation +
Logical && ||
Binary & | ^
Unary + - ! ~
Shift >> >>> <<
Comparison == > < >= <=
instanceof
Grouping ()
Literals - character, String, numeric, null
Cast
Method calls
Field access
Array access []
Ternary operator ?:
Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
12

You can also combine multiple conditions in this way

<androidx.appcompat.widget.AppCompatTextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@{sold_price == 0 ? (otherValue == 0 ? show_public_price : show_private_price) : (sold_price)}"
     android:textColor="@color/colorRed"
     android:textSize="@dimen/_12ssp" />
Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
7

If anyone wants to use the conditional statement as we do in "if" then below can be used in XML in any view.

app:visibleGone="@{model!=null && model.somevariable}"

replace above "&&" with "& amp;& amp"; (remove white space I intentially put here)

Anshul Agarwal
  • 1,513
  • 13
  • 17
5

For logical AND operation, use

"&amp ;&amp ;"

rather than && with no space, I give here.

android:visibility="@{viewModel.isCardSelected() &amp ;&amp ; !viewModel.isPaymentMethodEmpty() ? View.VISIBLE : View.GONE}"
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74