Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android data binding attribute not found

I am trying to replicate this answer: Setting attribute of child element of included layout

I have a simple custom_edit_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="hint123" type="String" />
    </data>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/emailInputLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/emailField"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:paddingBottom="16dp"
            android:paddingTop="14dp"
            android:hint="@{hint123}"
            android:textCursorDrawable="@null"
            android:background="@drawable/edit_text_background"
            android:fontFamily="@font/eina_regular"
            android:textColor="@color/edit_text_color"
            android:textColorHint="@color/edit_text_color"
            android:textSize="15sp"
            />
    </android.support.design.widget.TextInputLayout>
</layout>

And I include it in another file:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <include
            layout="@layout/custom_edit_text"
            app:hint123="Email"/>
</layout>

However the project refuses to compile after a clean & rebuild with the error:

AAPT: error: attribute hint123 (aka inc.company.appname:hint123) not found.

Any ideas?

I also have

dataBinding {
    enabled = true
}

enabled in the app level build.gradle

like image 702
Josh Avatar asked Mar 12 '19 03:03

Josh


People also ask

What is data binding in Android?

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. Layouts are often defined in activities with code that calls UI framework methods.

Can I use view binding and data binding together?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.


2 Answers

I think I've hit upon the solution. To activate data binding, you need to use a @{} expression, and what's in the braces must be valid Java code. So a literal string must be enclosed in quotes... which must be encoded as &quot; inside an XML attribute value. Put it all together and you get:

<include
    layout="@layout/custom_edit_text"
    app:hint123="@{&quot;Email&quot;}"/>

Data binding does work for include files, it's just that the syntax for a literal is a bit convoluted. I had the same issue and this form is working in my project.

like image 103
big_m Avatar answered Sep 18 '22 12:09

big_m


Bind the variables using @{ }

activity_main.xml

</layout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".simple.MainActivity">

        <include layout="@layout/content_main_data_binding"
            bind:name="@{ "Hello World" }" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

content_view.xml

</layout>

    <data>
    <variable
        name="name"
        type="String" />
    </data>

    <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{ name }"/>

 </layout>

GL

like image 31
Braian Coronel Avatar answered Sep 16 '22 12:09

Braian Coronel