Android - Conditional text value using Data Binding

23,866

Solution 1

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

Solution 2

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"
        />
Share:
23,866

Related videos on Youtube

Rajkiran
Author by

Rajkiran

Sr. Application Developer

Updated on July 09, 2022

Comments

  • Rajkiran
    Rajkiran almost 2 years

    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);
      }
    
    }
    
    • CommonsWare
      CommonsWare almost 8 years
      I thought that syntax worked. What problems are you encountering?
    • Ravi
      Ravi almost 8 years
      can you please post your xml file, something is missing in <data>
  • Himanshu
    Himanshu about 5 years
    How to achieve this using 2-way binding?
  • Andrey Turkovsky
    Andrey Turkovsky about 5 years
    What do you mean - "2-way binding"?
  • Himanshu
    Himanshu about 5 years
  • Andrey Turkovsky
    Andrey Turkovsky about 5 years
    I still don't understand your question. The answer was about using resources with conditions. I don't understand how do you want to use two-way binding with resources
  • Himanshu
    Himanshu about 5 years
    I wanted to ask if I make a change to the 'vm.editMode', how will the visibility be computed again. Is it possible?
  • Andrey Turkovsky
    Andrey Turkovsky about 5 years
    As you can see in another answer - yes, it's possible to change visibility the same way (if this is what you want)
  • Felipe
    Felipe about 4 years
    Needed to use @BindingMethods in my Fragment. But I'm working with ImageViews.

Related