25

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

Josh
  • 1,688
  • 4
  • 22
  • 35

6 Answers6

55

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.

big_m
  • 1,408
  • 1
  • 12
  • 24
  • Thanks a lot. This is a very strange behavior. I hope it will be fixed soon. – alexanderktx Feb 27 '20 at 11:02
  • 3
    ...or at least a descriptive error message that explains this simple issue – Daniel Wilson Jun 08 '20 at 15:22
  • Excellent explanation of what's going on here. Not sure I would have ever come up with this solution. It's unfortunate that there are no examples of this usage in the layout and bindings expressions docs: https://developer.android.com/topic/libraries/data-binding/expressions. – Christopher Pickslay Feb 01 '23 at 01:20
8

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

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
1

Problem is in the included layout. You can not set attribute hint123 in it.
Also, take a note that include is not supported as a direct child of layout tag.

Update your included XML code as below:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include android:id="@+id/custom_edit_text"
            layout="@layout/custom_edit_text" />
    </android.support.constraint.ConstraintLayout>

</layout>

To set hint using databinding, you have to set it in your java or kotlin file.

Here is java code:

public class YourActivity extends AppCompatActivity {

    YourActivityBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.your_activity);

        mBinding.customEditText.setHint123("Email");
        mBinding.customEditText.executePendingBindings();
    }
}
Viraj Patel
  • 2,113
  • 16
  • 23
  • Oh, I thought I was able to set the `hint123` attribute in XML. Would have been useful for reusing various layouts in the app. – Josh Mar 12 '19 at 04:34
  • https://stackoverflow.com/questions/9013298/setting-attribute-of-child-element-of-included-layout/44691776#44691776 this answer seems to suggest that you can send it a raw string – Josh Mar 12 '19 at 04:38
  • No, you can't. Check the accepted answer of the same question: https://stackoverflow.com/a/9013383/9293029 – Viraj Patel Mar 12 '19 at 04:39
  • don't forget to add the dataBinding element to your build.gradle file in the app module android { ... dataBinding { enabled = true } } – Amer Hadi Apr 20 '20 at 12:44
0

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

amitava
  • 505
  • 1
  • 5
  • 10
  • don't forget to add the dataBinding element to your build.gradle file in the app module android { ... dataBinding { enabled = true } } – Amer Hadi Apr 20 '20 at 12:44
0

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.

nayakasu
  • 889
  • 1
  • 11
  • 32
0

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`}"/>
Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92