'Cannot find setter for attribute app:visibleGone

I am trying to implement the MVVM architecture in my android application. I'm using Kotlin for the same.

This is my binding adapter class:

class BindingAdapter {
    companion object {
        @JvmStatic @BindingAdapter("app:visibleGone") fun showHide(view: View, show: Boolean) {
            view.visibility =
                    if (show)
                        View.VISIBLE
                    else
                        View.GONE
        }
    }
}

Here is my XML file:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
                name="isLoading"
                type="boolean"/>
    </data>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/cardview_light_background"
            android:orientation="vertical">

        <TextView
                android:id="@+id/loading_rates"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/loading_rates"
                android:textAlignment="center"
                app:visibleGone="@{isLoading}"/>

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/cardview_light_background"
                android:orientation="vertical"
                app:visibleGone="@{!isLoading}">

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="20dp"
                    android:layout_marginTop="20dp"
                    android:gravity="center_horizontal"
                    android:text="@string/rate_list"
                    android:textAlignment="center"
                    android:textSize="@dimen/rate_text"
                    android:textStyle="bold"/>

            <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rate_list"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layoutManager="LinearLayoutManager"/>

        </LinearLayout>

    </LinearLayout>
</layout>

The error message says:

Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:visibleGone' with parameter type boolean on android.widget.TextView. file:/home/sparker0i/CurrencyConverter/app/src/main/res/layout/fragment_rate_list.xml loc:23:35 - 23:43 ****\ data binding error ****

Line 23 resolves to the loading_rates TextView's line just above the app:visibleGone statement

I'm not able to understand that despite setting the BindingAdapter inside my Kotlin class, why am I not able to compile the code succesfully?



Solution 1:[1]

To resolve that problem check next things:

In root build.gradle you have

buildscript {
    ext.android_plugin_version = '3.1.2'
    dependencies {
        classpath "com.android.tools.build:gradle:$android_plugin_version"
    }
}

In app/build.gradle

apply plugin: 'kotlin-kapt'
dependencies {
    ...
    kapt "com.android.databinding:compiler:$android_plugin_version"
}

With all that stuff, problem should disappear and you can write @BindingAdapter for base classes and apply them to children of that base class.

Solution 2:[2]

What you can do is try hiding and showing the view in xml itself , you don't need a seperate binding adapter for it. I hope this answer's your problem.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
   <data>
       <import type="android.view.View" />

       <variable
            name="isLoading"
            type="boolean"/>
  </data>

    <TextView
            android:id="@+id/loading_rates"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/loading_rates"
            android:textAlignment="center"
            android:visibility="@{isLoading?View.VISIBLE:View.GONE}"/>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/cardview_light_background"
            android:orientation="vertical"
            android:visibility="@{isLoading?View.GONE:View.VISIBLE}">
 <layout>

Solution 3:[3]

Could you try without the app: prefix in BindingAdapter annotation and change your first parameter type:

@JvmStatic
@BindingAdapter("visibleGone")
fun showHide(view: TextView, show: Boolean) {
   view.visibility = if (show) View.VISIBLE else View.GONE
}

Solution 4:[4]

Before you try any complex solution, just verify that you have applied the following plugin in you app.gradle

apply plugin: 'kotlin-kapt'

If that is missing, it will keep giving errors. I found that missing from mine, after 8 hours of tussle and reading other SOF posts. It worked.

Adding picture for reference:

Adding picture for reference

Solution 5:[5]

I know here are already several answers, and all of them will work for different cases. But I recently faced with this issue and it took me some time to figure out that the first two things you have to do are:

  1. Check that you have apply plugin: 'kotlin-kapt' in your module gradle file;
  2. Check that you have dataBinding { enabled = true } in your module gradle file android {...} block.

For me the issue was in point 2.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ConstOrVar
Solution 2 Rajat Beck
Solution 3
Solution 4 sud007
Solution 5 Anton Prokopov