Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Data Binding using include tag

People also ask

How do you use data binding with tag?

Data Binding in <include> Layouts xml layout to enable data binding. The <data> and <variable> tags are used to bind the student object. To pass the user to included content_student_main layout, bind:student=”@{student}” is used. Without this, the user object won't be accessible in content_student_main layout.

What is include tag in Android?

Use the <include> tag Inside the layout to which you want to add the re-usable component, add the <include/> tag. For example, here's a layout that includes the title bar from above: Here's the layout file: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

Is data binding deprecated Android?

Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.


The problem is that the included layout isn't being thought of as a data-bound layout. To make it act as one, 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 buttons indirectly through the buttons field:

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

As of 1.0-rc4 (just released), you no longer need the variable. You can simplify it to:

buttons.xml:

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

main.xml:

<layout xmlns:andr...
...
   <include layout="@layout/buttons"
            android:id="@+id/buttons"/>
....

Easy Complete Example

Just set id to included layout, and use binding.includedLayout.anyView.

This example helps passing a value to <include & accessing included views in code.

Step 1

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

You will create String variable in layout and refer this String to 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 to parent layout. Give an id to included layout, so that we can use that in binding class. Now you can pass String 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>
  • You can use now binding.includedLayout.textView in your class.
  • You can pass any variables to included layout like above.

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    binding.includedLayout.textView.setText("text");
    

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


just set an id for your include layout

    <include
        android:id="@+id/layout"
        layout="@layout/buttons" />

then

BUTTONSBINDING binding = yourMainBinding.layout;

BUTTONSBINDING is res/layout/buttons.xml

now :

binding.button.setText("simple_Way");

An other interesting thing on this is that you can pas variables to the imported layout from the binder like this:

MainBinding binding = MainBinding.inflate(getLayoutInflater());
binding.buttons.setVariable(BR.varID, variable)

You can make your bind work on your include just adding a ID to it like so:

<include
            android:id="@+id/loading"
            layout="@layout/loading_layout"
            bind:booleanVisibility="@{viewModel.showLoading}" />