简体   繁体   中英

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

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.

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

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();
    }
}

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

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.

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`}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM