6

I get the following error: The value of attribute "type" associated with an element type "variable" must not contain the '<' character.

And type="ObservableField"/> is colored red in xml.

Any ideas?

 <?xml version="1.0" encoding="utf-8"?>
 <layout>

<data>

<import type="android.databinding.ObservableField"/>
<variable name="field" type="ObservableField<String>"/>

</data>

<android.support.constraint.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="72dp"
    android:text="@{field.get()}"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="181dp"
    android:layout_marginEnd="149dp"
    android:layout_marginStart="147dp"
    android:layout_marginTop="191dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>

</layout>
Nick
  • 2,818
  • 5
  • 42
  • 60
  • From what I have seen, you cannot use generics in data binding. Also, `ObservableField` would be invalid anyway, as [`ObservableField` takes only one type](https://developer.android.com/reference/android/databinding/ObservableField). Instead, bind a viewmodel as your ``, where that viewmodel has your `ObservableField`. Note that you do not need `get()` then in your binding expression. – CommonsWare Jul 14 '18 at 14:43
  • Does this answer your question? [Android Data binding, The value of attribute "android:text" associated with an element type "TextView" must not contain the '<' character](https://stackoverflow.com/questions/40970686/android-data-binding-the-value-of-attribute-androidtext-associated-with-an-e) – Tom Sabel May 12 '20 at 14:50

2 Answers2

16

A little late, but you can do it this by replacing the < and > for &lt; and &gt;, respectively.

Like this: <variable name="field" type="ObservableField&lt;String&gt;"/>

Renan Arceno
  • 590
  • 3
  • 12
  • 3
    This should be accepted answer. However, even Google's documentation shows the use of '<' and '>' within the type attribute. See: https://developer.android.com/topic/libraries/data-binding/observability#observable_Collections in the second code box you will see the use of '<' and '> in: ''. Is there any logical explanation here? – AutoM8R Jun 04 '19 at 20:32
2

try this instead

<?xml version="1.0" encoding="utf-8"?>
 <layout>

<data>

<variable name="field" type="android.databinding.ObservableField"/>

</data>

<android.support.constraint.ConstraintLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="72dp"
    android:text="@{field.get}"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="181dp"
    android:layout_marginEnd="149dp"
    android:layout_marginStart="147dp"
    android:layout_marginTop="191dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>

</layout>

and visit this for more details

Abdulmalek Dery
  • 996
  • 2
  • 15
  • 39