findusl
findusl

Reputation: 2644

Android Binding Adapter is not found

Please someone help me! I am going crazy, this should work. I have the following error message when I try to build my Android project:

Android resource linking failed
/Users/slehrbaum/StudioProjects/OneNightComps/Android/app/build/intermediates/incremental/mergeDebugResources/stripped.dir/layout/fragment_login.xml:17: error: attribute errorText (aka lehrbaum.de.onenightcomps:errorText) not found.
error: failed linking file resources.

the error message does mention the errorText attribute. I use the errorText attribute in the xml this way (full xml here):

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/usernameField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/username"
        app:hintEnabled="true"
        app:errorEnabled="true"
        app:errorText="Hi"
        >
        <!--app:errorText="Please provide a username."-->
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints="username"
            android:inputType="text"
            android:text="@={viewModel.username}"
            />
    </com.google.android.material.textfield.TextInputLayout>

This is the way I have defined errorText in my Kotlin file (full file here):

object ViewDataBindingExtensions {
    @JvmStatic
    @BindingAdapter("errorText")
    fun bindErrorText(textInputLayout: TextInputLayout, errorText: String) {
        textInputLayout.error = errorText
    }
}

I just don't understand why this happens. Is there some sort of import that I can put in the layout file saying where the BindingAdapter is? Do I have some wrong with my Gradle files? I compared it to the GitHub project in this question which apparently got solved and I do not see the difference to my project. According to the answer I should add the Kotlin-kapt plugin to my Gradle build, which I did. I also looked through the rest of the project and compared. To no avail. You can find my whole build.gradle file here as well as the rest of the project.

Please help me!

Upvotes: 9

Views: 8413

Answers (3)

nayakasu
nayakasu

Reputation: 969

Another reason which is not often discussed is you need to have your layout inside "" tag, else databinding doesn't work. I spent an hour for this.

Upvotes: 0

Anton Holovin
Anton Holovin

Reputation: 5673

The problem is connected with the way you pass String value to app:errorText.

Use @{``} to pass this value.

Fixed part of fragment_login.xml:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/username"
    app:hintEnabled="true"
    app:errorText="@{`Please provide a username.`}"
    app:errorEnabled="@{!viewModel.usernameValid}">

Having apply plugin: 'kotlin-kapt' in app/build.gradle is mandatory.

Upvotes: 46

Vahalaru
Vahalaru

Reputation: 400

Try using

fun bindErrorText(textInputEditText: TextInputEditText, errorText: String) {
 textInputEditText.error = errorText }

Upvotes: 0

Related Questions