Syntax for conditional statements in Android XML Layout
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 ?:
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" />
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