As per http://developer.android.com/tools/data-binding/guide.html#imports, we can have such simple expressions in visibility:
You can find the included layout by its id and hide it using the visibility
attribute.
For example in Kotlin,
binding.root.findViewById<CardView>(R.id.layout_inclulded).visibility = View.GONE
where,
root
is the default attribute for the root element in the parent layout.
CardView
is the root element of the included layout in my case.
R.id.layout_inclulded
is the id
of the included layout in the parent layout.
Try:
this.imageLayout.getRoot().setVisibility(NotifTypeNotificatio1);
Integer
value of visibility
like.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="int"/>
</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>
Thank to everybody, you help me a lot. Your answers were part of my succes on this trouble.
We wanted to set visibility of an image view, that was also target of some animation. The Motion Scene was taking the management of the visibility. Set the motion property app:visibilityMode to "ignore" solve everything.
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ConstraintSet android:id="@+id/expanded">
<!-- ... -->
<Constraint
android:id="@+id/myImageViewId"
app:visibilityMode="ignore"/>
</ConstraintSet>
<!-- ... -->
</MotionScene>
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>