Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Conditional text value using Data Binding

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);   }  } 
like image 202
Rajkiran Avatar asked Jun 09 '16 12:06

Rajkiran


2 Answers

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

like image 105
Andrey Turkovsky Avatar answered Sep 22 '22 19:09

Andrey Turkovsky


Here's a fix/work-around :

  1. define a duplicate Xml definition of the layout where you want a conditional value
  2. for each block, set one of the condition values
  3. set the visibility of each Xml definition according to the data binding boolean value

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"         /> 
like image 26
Gene Bo Avatar answered Sep 21 '22 19:09

Gene Bo