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();
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}"/>
You are able to pass all necessary parameters from parent xml into included xml via "http://schemas.android.com/apk/res-auto"
namespace.
The syntax is res-auto namespace:variable name
For example
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/include_user_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:isVisible="@{true}" />
</android.support.design.widget.CoordinatorLayout>
</layout>
include_user_image.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
This is a bit late, but I have run across this recently.
I believe this is actually a bug in the Data Binding compiler as it is possible to set android:visibility
attribute on <include>
tag directly (ie. without Databinding).
Good explaination
- You can directly pass
Integer
value of visibility
like.
- You can set default value of
visibility
also by setting default=gone
in binding.
For example this is included_layout.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="visibility"
type="Integer"/>
</data>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{visibility, default=gone}"
/>
</layout>
and use like
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View"/>
</data>
<include
android:id="@+id/included_layout"
layout="@layout/included_layout"
app:visibility="@{View.VISIBLE}"/>
</layout>
Try:
this.imageLayout.getRoot().setVisibility(NotifTypeNotificatio1);