I want to set the text of my TextView conditionally to either one or the other.
Android Data Binding documentation suggests that you can set the text conditionally if the text is a property of the view model. e.g.
android:text="@{user.displayName != null ? user.displayName : user.lastName}"
But is there any way to set the text from the strings.xml rather than adding it in my view model? I want something like this-
android:text="@{viewModel.expanded ? @string/collapse : @string/expand}"
The XML looks somewhat like this:
<?xml version="1.0" encoding="UTF-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bind="http://schemas.android.com/apk/res-auto"> <data class="TravellerInfoBinding"> <import type="android.view.View" /> <variable name="viewModel" type="com.myproject.viewmodel.TravellerInfoViewModel" /> </data> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@drawable/expandable_arrow_blue" /> <TextView style="@style/primary_pair_element_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{viewModel.expanded ? @string/taxes_fees_detail : @string/hide_taxes_fees_detail}" android:textSize="12sp" /> </LinearLayout> </layout>
And this is my View Model-
package com.myproject.viewmodel; imports... public class TravellerInfoViewModel extends BaseObservable { @Bindable private final TaxDetailsViewModel taxDetailsViewModel; @Bindable private boolean expanded; Constructor.... public boolean isExpanded() { return expanded; } public void setExpanded(boolean expanded) { this.expanded = expanded; notifyPropertyChanged(BR.expanded); } public void toggleExpanded() { setExpanded(!expanded); } }
Actually, this works fine for me
<TextView android:id="@+id/btnEdit" style="@style/Common.Toolbar.Action.Text" android:onClickListener="@{onEditClick}" android:text="@{vm.editMode ? @string/contacts_done : @string/contacts_edit}" tools:text="@string/contacts_edit"/>
Where vm
- it's a ViewModel and editMode
- it's ObservableBoolean
Here's a fix/work-around :
Not the ideal solution, not very pretty .. but functionally equivalent - and works in the interim until proper solution is found.
Here's how I solved it for android:textStyle
, where I had a special case requirement for showing values in bold.
<variable name="viewModel" type="com.demo.app.SomeViewModel"/> ... <TextView style="@style/RowValue" android:visibility="@{ ! viewModel.boldRow ? View.VISIBLE : View.GONE}" android:text="@{viewModel.currentValue}" /> <TextView style="@style/RowValue" android:visibility="@{ viewModel.boldRow ? View.VISIBLE : View.GONE}" android:text="@{viewModel.currentValue}" android:textStyle="bold" />
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