Android data binding using include tag

Note update:

The above example works correctly because the 1.0-rc4 release fixes the need to use an unnecessary variable.

The original question:

I do exactly as described in the documentation, and this does not work:

main.xml:

<layout xmlns:andr... <data> </data> <include layout="@layout/buttons"></include> .... 

buttons.xml:

 <layout xmlns:andr...> <data> </data> <Button android:id="@+id/button" ...." /> 

MyActivity.java:

  ... binding = DataBindingUtil.inflate... binding.button; ->cannot resolve symbol 'button' 

how to get a button?

+72
java android data-binding android-button android-databinding
05 Oct '15 at 11:25
source share
3 answers

The problem is that the included layout is not considered a data-bound layout. To make it act as a unit, you need to pass a variable:

buttons.xml:

 <layout xmlns:andr...> <data> <variable name="foo" type="int"/> </data> <Button android:id="@+id/button" ...." /> 

main.xml:

 <layout xmlns:andr... ... <include layout="@layout/buttons" android:id="@+id/buttons" app:foo="@{1}"/> .... 

Then you can access the buttons indirectly through the button field:

 MainBinding binding = MainBinding.inflate(getLayoutInflater()); binding.buttons.button 

Starting with 1.0-rc4 (just released), you no longer need a variable. You can simplify this to:

buttons.xml:

 <layout xmlns:andr...> <Button android:id="@+id/button" ...." /> 

main.xml:

 <layout xmlns:andr... ... <include layout="@layout/buttons" android:id="@+id/buttons"/> .... 
+138
Oct 05 '15 at 21:59
source share
— -

Easy Completed Example

Just set the id for the included layout and use binding.includedLayout.anyView .

This example helps pass the value of <include and access the included views in the code.

Step 1

You have layout_common.xml , you want to pass String to the included layout.

You will create a String variable in the layout and pass that String to the TextView .

 <data> // declare fields <variable name="passedText" type="String"/> </data> <TextView android:id="@+id/textView" ... android:text="@{passedText}"/> //set field to your view. 

Step 2

Include this layout in the parent layout. Assign an id included layout so that we can use it in the binding class. Now you can pass passedText to your <include tag.

activity_main.xml

 <?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 .. > <include android:id="@+id/includedLayout" layout="@layout/layout_common" app:passedText="@{@string/app_name}" // here we pass any String /> </LinearLayout> </layout> 
  • Now you can use binding.includedLayout.textView in your class.
  • You can pass any variables to the included layout as described above.

    ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater()); binding.includedLayout.textView.setText("text");

Note- that both layouts (parent and included) must be binding layout wrapped in <layout

+10
Aug 01 '18 at 16:56
source share

Another interesting thing is that you can use pas variables for the imported layout from this binder:

 MainBinding binding = MainBinding.inflate(getLayoutInflater()); binding.buttons.setVariable(BR.varID, variable) 
+1
Jul 22 '17 at 20:51
source share



All Articles