简体   繁体   中英

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

Add this to your cardview_image_twotextview.xml :

<data>
    <import type="android.view.View" />
    <!--your variables-->
</data>

Zero or more import elements may be used inside the data element. These allow easy reference to classes inside your layout file, just like in Java.

you need to import View class inorder to use its properties.

<data>
    <import type="android.view.View"/>
</data>

you can also refer official DataBinding guideline .

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}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM