Viewbinding with error: incompatible with attribute android:visibility
Asked Answered
G

6

8

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.

Layout:

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

Data class

@Parcelize
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?

Gleam answered 8/11, 2018 at 7:3 Comment(3)
Are you certain that viewData.getIconVisibility() is a boolean?Seaware
The variable viewData type is of SomethingViewData class and the data class which you posted is GetStartedViewData. Can you confirm if the type refers to the data same data class you posted?Piraeus
@Guatam, editedGleam
E
44

In my case a missing closing brace caused this error to pop up for me - I had:

android:visibility="@{moment.state == State.COMPLETE ? View.GONE : View.VISIBLE"

instead of:

android:visibility="@{moment.state == State.COMPLETE ? View.GONE : View.VISIBLE}"
Evangelina answered 5/7, 2019 at 0:58 Comment(2)
Thanks man, surprisingly relevant. Took me googling and clicking through to this answer to notice that missing brace :facepalm:Runnels
Android studio is dumb enough not to remind a missing brace but attribute incompatibility.Elegit
K
14

I had the same error and solved it with help of this site: https://codelabs.developers.google.com/codelabs/android-databinding/#2

I needed to convert my ConstraintLayout to a data binding layout like shown on this picture from this site: enter image description here

Maybe this will help someone!

Kedron answered 10/1, 2020 at 12:57 Comment(1)
Thank you a lot, this was the easiest way to deal with data binding in KotlinHighwayman
G
11

Wow, so, somehow dataBinding { enabled = true } was removed from my app modules build.gradle file. Adding it back and everything worked like before.

Gleam answered 9/11, 2018 at 7:18 Comment(2)
The same happened to me.Hord
It should now be buildFeatures{ dataBinding true}Ash
C
0

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

Cannabin answered 8/11, 2018 at 7:11 Comment(4)
Ah yes, I've been trying that too. Changed to boolean, and did this in the layout android:visibility="@{viewData.getIconVisibility() ? View.VISIBLE : View.GONE}", but i still get the errorGleam
You have the same error with "android:src". Did you resolve it?Cannabin
I have not figured it out yet, noGleam
ContextCompat.getDrawable(context, viewData.getIconResource()) -> this will help you create a drawable object but it's not used for XML file!!! In your case may be: android:src="@{viewData.getIconResource()}"Cannabin
S
0

Try using single quotations in your src and visibility

android:src='@{ContextCompat.getDrawable(context, viewData.getIconResource())}'
android:visibility='@{viewData.getIconVisibility() ? View.VISIBLE : View.GONE}'
Seaware answered 8/11, 2018 at 7:31 Comment(2)
What I dont understand is that there are others that work. android:text="@{viewData.getTitleString()}" for example.Gleam
Single quotes didnt fix, sorryGleam
M
0

As of 2023 I just had to use it like this:

android:visibility="gone"

According to the documentations the possible values for the layout XML are:

android:visibility="visible|invisible|gone"

You can check the documentation here: Android View Documentation

Manager answered 26/6, 2023 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.