Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Data Binding: how to pass variable to include layout

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.

like image 953
pmellaaho Avatar asked Feb 09 '16 08:02

pmellaaho


People also ask

Which is the correct way to reference bound data in the XML layout?

Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression. Expressions can be used for many purposes depending on your requirement.

What is ActivityMainBinding in Android?

xml so the corresponding generated class is ActivityMainBinding . This class holds all the bindings from the layout properties (for example, the user variable) to the layout's views and knows how to assign values for the binding expressions.

Can I use both data binding and view binding?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.

What is Databindingutil in Android?

Inflates a binding layout and returns the newly-created binding for that layout. static <T extends ViewDataBinding> T. inflate(LayoutInflater inflater, int layoutId, ViewGroup parent, boolean attachToParent) Inflates a binding layout and returns the newly-created binding for that layout.


3 Answers

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

like image 74
Khemraj Sharma Avatar answered Oct 17 '22 02:10

Khemraj Sharma


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>
like image 36
Batgard Avatar answered Oct 17 '22 01:10

Batgard


For hardcoded string:

 android:label="@{`Test 123`}"
like image 20
Gene Bo Avatar answered Oct 17 '22 01:10

Gene Bo