Is there any way to write multi-line code with DataBinding library?
I tried following, but it complains about XML format
android:text="@{ viewModel.subscriptionExpiration == null ? viewModel.field1 : viewModel.field2 }"
With following error: Error:(80) Error parsing XML: not well-formed (invalid token)
Single-line alternative works, but I find it less readable:
android:text="@{viewModel.subscriptionExpiration == null ? viewModel.field1 : viewModel.field2}"
Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression. Expressions can be used for many purposes depending on your requirement.
Data Binding in <include> Layouts xml layout to enable data binding. The <data> and <variable> tags are used to bind the student object. To pass the user to included content_student_main layout, bind:student=”@{student}” is used. Without this, the user object won't be accessible in content_student_main layout.
Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.
I tried doing some digging and couldn't find an answer. I don't know that you can break up string literals that way in xml. As an alternative I would recommend offloading the evaluation to the viewModel instead, in the form of:
android:text="@{viewModel.subscriptionStatus}"
then in your viewModel class
@Bindable public String getSubscriptionStatus(){ return getSubscriptionExpiration() == null ? getField1() : getField2(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With