With data binding, we now often see codes in layout files, for example:
<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 in order to make any sense)
This simplifies viewing management. It also tells me that conditional statements are allowed in the XML layout, but it looks like my google-fu is weak, I tried and could not find the syntax for this. What if I want to use literals? Sort of:
android:text="{@isValid ? "valid" : "invalid"}"
(yes, I know this is a dumb way to do this, I'm just talking about the syntax here). Or what about resource identifiers? Maybe like:
android:color="@{isValid ? R.color.green : R.color.red}"
Can this be done? What is the correct syntax?
source share