Reputation: 1718
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
Upvotes: 27
Views: 15816
Reputation: 4580
This error can also occur when <layout>
tag is not placed upon the calling layout.
(the xml within which we're using the <include>
tag and setting a binding attribute value as bind:myvariable or app:myvariable)
PS: Not the case presented in this issue, but just something to be wary of.
Upvotes: 0
Reputation: 17772
There are 2 alternatives to the quote-escaping solution @big_m provides.
You can single-quote the entire expression and use double-quotes around the string:
<include
layout="@layout/custom_edit_text"
app:hint123='@{"Email"}'/>
Or you can use backticks around the string:
<include
layout="@layout/custom_edit_text"
app:hint123="@{`Email`}"/>
Upvotes: 3
Reputation: 969
Just in case anyone misses one detail like me: you must enclose your parent layout content and the included layout content within "" and "". Without that binding is not used in a layout. I had the same issue when I had not enclosed my parent layout with these tags.
Upvotes: 0
Reputation: 22905
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
Upvotes: 8
Reputation: 1448
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.
Upvotes: 57
Reputation: 505
I am using Android 3.1.1. And the following code is working for me, and if you can use it you will be able to reuse "hint" as you desired. I have a slightly altered layout file (custom_edit_text.xml) as follows.
<?xml version="1.0" encoding="utf-8"?>
<data>
<variable name="cName" type="String" />
<variable name="user" type="your.package.name.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.email}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.mobile}" />
<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:layout_width="match_parent"
android:layout_height="50dp"
android:hint="@{cName.toString()}"
android:paddingBottom="16dp"
android:paddingTop="14dp"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Above the second "type" is package name + User class name.
I create the "User" class in a separate file as follows.
public class User {
String email;
String mobile;
User(String email, String mobile) {
this.email = email;
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public String getMobile() {
return mobile;
}
}
Inside MainActivity inside onCreate() I create the user object create the string and bind them.
String email = "xyz@yahoo";
String mobile = "9999";
User user = new User(email,mobile);
CustomEditTextBinding binding = DataBindingUtil.setContentView(this,R.layout.custom_edit_text) ;
binding.setCName("Yam May");
binding.setUser(user);
And I enabled binding in the app level build.gradle as you did.
A very detailed description about data binding can be found in https://www.vogella.com/tutorials/AndroidDatabinding/article.html
Upvotes: 0