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.
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.
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.
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.
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.
Just create
<variable
for passing values to included layouts.Like
app:passedText="@{@string/app_name}"
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
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>
For hardcoded string:
android:label="@{`Test 123`}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With