Syntax for conditional statements in Android XML Layout

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 … Read more

Android spinner Data Binding using XML and show the selected values

1 Line Solution android:selectedItemPosition=”@={item.selectedItemPosition}” That’s it! No need to make custom BindingAdapter. Spinner already supports two-way binding by attributes selection and selectedItemPosition. See Android Documentation You just need to use two way binding selectedItemPosition so that change on spinner reflect on your model field. Example Item.class public class Item extends BaseObservable { private int selectedItemPosition; … Read more

Create two-way binding with Android Data Binding

EDIT 04.05.16: Android Data binding now supports two way-binding automatically! Simply replace: android:text=”@{viewModel.address}” with: android:text=”@={viewModel.address}” in an EditText for instance and you get two-way binding. Make sure you update to the latest version of Android Studio/gradle/build-tools to enable this. (PREVIOUS ANSWER): I tried Bhavdip Pathar’s solution, but this failed to update other views I had … Read more

data binding – safeUnbox warning

I had the same warning, in my case changing the variable declaration from Boolean type to boolean type solve the problem: From: <variable name=”readOnly” type=”Boolean” /> To: <variable name=”readOnly” type=”boolean” /> So, maybe you can try with: <variable name=”selectMap” type=”android.databinding.ObservableMap&lt;Integer, boolean&gt;” />

tech