0

As George Mount said starting from 1.0-rc4 we no longer need the variable in include when using data binding:

buttons.xml:

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

main.xml:

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

But I tried it and get error:

Error:(10, 31) Identifiers must have user defined types from the XML file. toolbarViewModel is missing it

I have the included toolbar:

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

    <data class="LoginPhoneFragmentBinding">

        <variable
            name="toolbarViewModel"
            type="ru.mobileup.myalarm2.binding.ToolbarViewModel"/>
    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

And toolbar layout is:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/primary"
    android:theme="@style/ToolbarThemeOverlay"
    app:navigationIcon="@{toolbarViewModel.navigationIconResId}"
    app:navigationOnClickListener="@{toolbarViewModel.navigationOnClickListener}"
    app:title="@{toolbarViewModel.title}"
    app:menu="@{toolbarViewModel.menuResId}"
    app:menuListener="@{toolbarViewModel.onMenuItemClickListener}"/>

What is wrong?

Note: I know that with passed variable all works fine. I trying to figure out how use that George mentioned.

Community
  • 1
  • 1
Jeevuz
  • 13
  • 1
  • 8
  • Are you using the latest version of the databinding library..? The implementation has been [changed](https://developer.android.com/tools/data-binding/guide.html#build_environment). Please also post the relevant `Java`code. :) – yennsarah Feb 15 '16 at 14:19
  • Hi, I use the 2.0.0-beta4 plugin, and switch binding on using `dataBinding { enabled = true }`. So I think this is the latest for now. @Amy, I think java is not needed here, because you can reproduce it with just toolbar title passed in first layout as ObservableField, and you will see the same result. – Jeevuz Feb 16 '16 at 13:45
  • Doesn't work for me either – Ishaan Garg Mar 01 '16 at 14:11

1 Answers1

0

I think you're misunderstanding George's answer on the linked post; you still need the variable if you want to reference it with @{toolbarViewModel.title} as you do in your included toolbar layout.

If you have dataBinding enabled in your build.gradle and wrap your layout in an extra <layout> tag you'll get an automatically generated ViewDataBinding class that contains resolved references to any views that have IDs in your layout (e.g. binding.toolbar, or something like that). This won't automatically bind your data for you, but will allow you to get rid of any findViewById calls.

See his blog post here for more information.

jbluntz
  • 78
  • 7
  • Yep, I think you are right. This was misunderstanding. Just read his answer again and now I see that he is talking only about the ids and from 1.0-rc4 there is just no need to pass variable to get the view by id as binding.buttons.button1 as it was before the mentioned version. – Jeevuz Jul 14 '16 at 16:00