Databinding error: Only one layout element and one data element are allowed.

In < layout>, must have one < data> and one layoutView(Relative/Linear etc).... Multiple layoutViews are not allowed,In layoutViews may have multiple layoutViews but on top layer multiplicity are not allowed....


I solved my issue. This error appears when there is more than single element in the layout tag:

Wrong:

<layout>
     <data>
          ...
     </data>
     <LinearLayout>
          ...
     </LinearLayout>
     <LinearLayout>
          ...
     </LinearLayout>
</layout>

Correct:

<layout>
     <data>
          ...
     </data>
     <LinearLayout>
         <LinearLayout>
              ...
         </LinearLayout>
         <LinearLayout>
              ...
         </LinearLayout>
     </LinearLayout>
</layout>

Also this error hapens when you are using DataBinding and using tag without tag

correct

<layout>
 <data>
      <variable>
      </variable>
 </data>
 <LinearLayout>
      ...
 </LinearLayout>
 <LinearLayout>
      ...
 </LinearLayout>

in other words you have to keap DataBinding structure in xml


  • ViewBinding only one layout element is allowed.
  • DataBinding only one layout element and one data element are allowed.

build.gradle (:mobile)

android {
    //...

    buildFeatures{
        viewBinding = true
    }
}

Do not use android:layout_width or android:layout_height as attributes of the <layout/> tag because otherwise dataBinding already counts it as a view and then in the scope of layot we could not put another view.

test.xml

<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">

    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- ... -->

    </View>

</layout>

GL