Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visibility not working in data binding android

I am using the latest data binding in android using android studio 2.1. using the visibility tag as describe in the below code getting an error as

java.lang.RuntimeException: Found data binding errors. / data binding error ****msg:Identifiers must have user defined types from the XML file. View is missing it file:D:\HP\HealthPortal_Android\Code\app\src\main\res\layout\cardview_image_twotextview.xml loc:68:90 - 68:93 \ data binding error

   <TextView
                        android:id="@+id/card_sub_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/card_title"
                        android:layout_marginLeft="@dimen/carview_margin"
                        android:layout_toRightOf="@+id/card_image"
                        android:text="@{toolsAndTrackersCards.subtitle}"
                        android:textColor="@color/black"
                        android:textSize="20sp"
                        android:visibility="@{toolsAndTrackersCards.subtitle.equals(@string/Empty_String) ?  View.VISIBLE : View.GONE}"
                        />

Done some google not abel to find the solution. The @string/Empty_String is define as empty string "" in string.xml file. where I am doing wrong.

Android data binding, Radio Button not updating

like image 547
San Jaisy Avatar asked Apr 29 '16 12:04

San Jaisy


1 Answers

To hide a view if the string is empty, use the below expression in data binding

<data>
    <import type="android.view.View"/>
    <variable
        name="item"
        type="com.test.model.Item" />
</data>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{item.title}"
    android:visibility='@{item.title.equals("") ? View.GONE : View.VISIBLE}'/>

NOTE: need to use outer single quote string in order to use double quote to represent the empty string

If you want to check for null and empty, use the below code :

<data>
    <import type="android.view.View"/>
    <import type="android.text.TextUtils"/>
    <variable
        name="item"
        type="com.test.model.Item" />
</data>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{item.title}"
    android:visibility="@{TextUtils.isEmpty(item.title) ? View.GONE : View.VISIBLE}"/>
like image 153
Darshna Desai Avatar answered Oct 19 '22 11:10

Darshna Desai