Search code examples
androidandroid-databinding

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}"/>

Solution

  • 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}"/>