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
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.
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.
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 "
inside an XML attribute value. Put it all together and you get:
<include
layout="@layout/custom_edit_text"
app:hint123="@{"Email"}"/>
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.
Bind the variables using @{ }
</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>
</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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With