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.

pmellaaho
  • 792
  • 1
  • 8
  • 15

3 Answers3

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

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
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>
Batgard
  • 156
  • 1
  • 4
  • 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. – pmellaaho Feb 22 '16 at 07:11
  • 1
    Is it possible to pass multiple variables? I've tried: ` ` but that doesn't seem to work. – b.lyte Sep 25 '16 at 15:37
9

For hardcoded string:

 android:label="@{`Test 123`}"
Gene Bo
  • 11,284
  • 8
  • 90
  • 137