8

Is there a way to pass data variable to included layout?

parent layout

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="@layout/fragment_settings">

    <data>

        <variable
            name="viewModel"
            type="......settings.SettingsFragmentViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:overScrollMode="never"
        android:padding="@dimen/spacing_default">

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

    </androidx.constraintlayout.widget.ConstraintLayout>

and want to have viewModel in included layout

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="@layout/fragment_settings">

    <data>

        <variable
            name="viewModel"
            type="......settings.SettingsFragmentViewModel" />
    </data>

    <merge>
...........

or is it possible only when setting like this:

 binding = FragmentSettingsBinding.inflate(inflater, container, false).apply {
            lifecycleOwner = this@SettingsFragment
            viewModel = [email protected]
            main.viewModel = [email protected]
        }

3 Answers 3

15

In parent xml inside include tag pass data variable to included layout using bind:viewModel.

<?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"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-auto"
tools:showIn="@layout/fragment_settings">

<data>

    <variable
        name="viewModel"
        type="......settings.SettingsFragmentViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:overScrollMode="never"
    android:padding="@dimen/spacing_default">

    <include
        android:id="@+id/main"
        layout="@layout/layout_settings_main"
        bind:viewModel="@{viewModel}" />

</androidx.constraintlayout.widget.ConstraintLayout>

Your included layout will get object instance in viewModel.

For more details check this tutorial

1
  • What is the difference if I use app:ViewModel or bind:viewModel?
    – Astha Garg
    Commented Aug 5, 2022 at 9:16
0

Open build.grade and add dataBinding { enabled = true } in block android{ // ...}

2
  • 1
    databinding is already working. Please read the question first Commented Oct 3, 2019 at 8:43
  • I thought databinding isn't already working because don't see your config in file build.grade. Sorry :(
    – ngdathd
    Commented Oct 3, 2019 at 8:53
0

Ok so, at the beginning of this article I’ve said that it depends whether it’s possible to pass the variable to a secondary layout or not. The reason for this is the following: Data binding does not support include as a direct child of a merge element. For example, the following layout is not supported:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto">
   <data>
       <variable name="user" type="it.communikein.myunimib.User"/>
   </data>
   <merge>
       <include layout="@layout/name"
           bind:user="@{user}"/>
       <include layout="@layout/contact"
           bind:user="@{user}"/>
   </merge>
</layout>
2
  • and what is supported? Commented Oct 3, 2019 at 8:24
  • The <data> tag includes a <variable> tag that has the same name that we used in the main layout to pass the data to the secondary layout: secondary user. Also, it has the same type of variable declared in the main layout. Commented Oct 3, 2019 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.