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();
Solution 1:
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}"/>
Solution 2:
Try:
this.imageLayout.getRoot().setVisibility(NotifTypeNotificatio1);