Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Data Binding: visibility on include tag

As per http://developer.android.com/tools/data-binding/guide.html#imports, we can have such simple expressions in visibility:

<TextView .. android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"/> 

But when I try to do the same in an include tag, like so:

<include android:id="@+id/image_layout" layout="@layout/image_layout" android:visibility="@{notification.notifType == 0 ? View.VISIBLE : View.GONE}"/> 

Then Studio not only shows the expression in red, but upon building it gives the following error in the auto-generated binding class:

Error:(138, 29) error: cannot find symbol method setVisibility(int)

Here's where the error occurs in the auto-generated binding class

// batch finished if ((dirtyFlags & 0x3L) != 0) {     // api target 1     this.imageLayout.setVisibility(NotifTypeNotificatio1); } imageLayout.executePendingBindings(); 
like image 493
Ishaan Garg Avatar asked Mar 01 '16 13:03

Ishaan Garg


People also ask

Can we use both Data Binding and view Binding?

There is nothing ViewBinding can do that DataBinding cannot but it costs longer build times. Remember you don't need to use both, if you are using DataBinding, there is no need adding ViewBinding.

What is Android bindable annotation?

The Bindable annotation should be applied to any getter accessor method of an Observable class. Bindable will generate a field in the BR class to identify the field that has changed. When applied to an accessor method, the Bindable annotation takes an optional list of property names that it depends on.

How check Data Binding is null in Android?

Generated data binding code automatically checks for null values and avoid null pointer exceptions. For example, in the expression @{user.name} , if user is null, user.name is assigned its default value of null . If you reference user. age , where age is of type int , then data binding uses the default value of 0 .


2 Answers

I imagine what you are trying to do would look something like this:

In the layout you are including, specify a boolean variable and bind it to the desired view's visibility

<layout xmlns:android="http://schemas.android.com/apk/res/android">      <data>          <import type="android.view.View"/>          <variable             name="isVisible"             type="boolean"/>      </data>      <ImageView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:visibility="@{isVisible ? View.VISIBLE : View.GONE}"/>  </layout> 

Then In your calling layout bind your value

<include     android:id="@+id/image_layout"     layout="@layout/image_layout"     bind:isVisible="@{notification.notifType == 0}"/> 
like image 186
SeptimusX75 Avatar answered Sep 18 '22 14:09

SeptimusX75


Try:

this.imageLayout.getRoot().setVisibility(NotifTypeNotificatio1); 
like image 22
julzborrego Avatar answered Sep 19 '22 14:09

julzborrego