Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 441
Sparker0i Avatar asked Nov 05 '18 19:11

Sparker0i


Video Answer


1 Answers

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.

like image 126
ConstOrVar Avatar answered Sep 28 '22 04:09

ConstOrVar