Viewbinding with error: incompatible with attribute android:visibility

I'm trying to set the visibility and the src of an ImageView using data binding. I don't know why this error is showing up, and I truely believe that this was working at one point, but I could be wrong.


    <import type="android.view.View" />
    <import type="" />
        type="com.myapp.SomethingViewData" />
            android:src="@{ContextCompat.getDrawable(context, viewData.getIconResource())}"
            android:visibility="@{viewData.getIconVisibility() ? View.VISIBLE : View.GONE}" />

Data class

data class SomethingViewData(val iconResource: Int,
                             val iconVisibility: Boolean) : Parcelable

Error message:

error: '@{ContextCompat.getDrawable(context, viewData.getIconResource())}' is incompatible with attribute android:src (attr) reference|color.   
error: '@{viewData.getIconVisibility() ? View.VISIBLE : View.GONE}' is incompatible with attribute android:visibility (attr) enum [gone=2, invisible=1, visible=0]. 

What does this mean and how do I fix it?

2 answers

  • answered 2018-11-08 07:11 Louis Solo

    Your "getIconVisibility" return an integer but android:visibility (in your XML file) need a enum value: Visibility.Gone | Visibility.Visible | Visibility.Invisible

  • answered 2018-11-08 07:31 jake

    Try using single quotations in your src and visibility

    android:src='@{ContextCompat.getDrawable(context, viewData.getIconResource())}'
    android:visibility='@{viewData.getIconVisibility() ? View.VISIBLE : View.GONE}'