Use data binding to set View visibility
Trying to set visibility of View
using custom variable, but error occurs: Identifiers must have user defined types from the XML file. visible is missing it
. Is it possible to set view visibility using data binding? Thanks.
<data>
<variable
name="sale"
type="java.lang.Boolean"/>
</data>
<FrameLayout android:visibility="@{sale ? visible : gone}"/>
As stated in the Android Developer Guide, you need to do it like this:
<data>
<import type="android.view.View"/>
<variable
name="sale"
type="java.lang.Boolean"/>
</data>
<FrameLayout android:visibility="@{sale ? View.GONE : View.VISIBLE}"/>
In your layout:
<data>
<variable
name="viewModel"
type="...."/>
</data>
<View
android:layout_width="10dp"
android:layout_height="10dp"
android:visibility="@{viewModel.saleVisibility, default=gone}"/>
In your ViewModel java code:
@Bindable
public int getSaleVisibility(){
return mSaleIndecator ? VISIBLE : GONE;
}