33

Google documentation says that variables may be passed into an included layout's binding from the containing layout but I can't make it work but get data binding error ****msg:Identifiers must have user defined types from the XML file. handler is missing it. The including XML looks like this:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto">

<data>
    <import type="com.example.FocusChangeHandler"/>

    <variable
        name="handler"
        type="FocusChangeHandler"/>
</data>

<!-- Some other views  --->

   <include
            android:id="@+id/inputs"
            layout="@layout/input_fields"
            bind:handler="@{handler}"/>        
</layout>

And the included XML like this:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
   android:id="@+id/nameEdit"       
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"       
   android:onFocusChange="@{handler.onFocusChange}"/>
</layout>

I'm able to refer the Views from included layout through generated binding class but passing a variable just doesn't work.

3 Answers 3

25

Just create <variable for passing values to included layouts.

Like app:passedText="@{@string/app_name}"

Example

Like I want to pass String to included layout. I will create a variable of type String. Refer that String to your TextView. I created passedText for example.

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

    <data>
        // declare fields
        <variable
            name="passedText"
            type="String"/>
    </data>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{passedText}"/> //set field to your view.

</layout>

Now add passedText field to your <include tag.

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <include
            layout="@layout/layout_common"
            app:passedText="@{@string/app_name}" // here we pass any String 
            />

    </LinearLayout>
</layout>

Note that both layouts (parent & included) should be binding layout, wrapped with <layout

Sign up to request clarification or add additional context in comments.

6 Comments

Remember to call DataBindingUtil.setContentView<FragmentOfferDetailBinding>(this, R.layout.fragment_offer_detail), EVEN if you're not using any of its elements.
@DelacrixMorgan See stackoverflow.com/a/51633632/6891563, please don't use DataBindingUtil for Fragments & Dialogs.
Simple and crisp!
@CosmicDev yes you can take multiple strings variables, there is no limit.
This doesn't work for me. Is there anything else im missing except for these 2 code snippets? Do fragments (kotlin) files need something extra?
|
14

The documentation specifies

Here, there must be a user variable in both the name.xml and contact.xml layout files

I assume you should have this in your included layout:

    <data>
           <variable name="handler"
                     type="FocusChangeHandler"/>
    </data>

2 Comments

Funny but I already tried this when I was playing with it and got an error about variable being defined multiple times. Something must have been changed between Android Studio 2.0 Beta 3 and Beta 5 as it seems to work now.
Is it possible to pass multiple variables? I've tried: <variable name="onClick" type="android.view.View.OnClickListener"/> <variable name="buttonText" type="String"/> but that doesn't seem to work.
9

For hardcoded string:

 android:label="@{`Test 123`}"

1 Comment

Not working for me. attribute not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.