Search code examples
androiddata-binding

Android data binding - 'No resource identifier found for attribute'


My layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

<TextView
      android:text="@string/hello_world"
      android:layout_width="wrap_content"
      app:fontName="Roboto-Regular.ttf"
      android:layout_height="wrap_content"/>

</RelativeLayout>

My binding adapter method:

public class FontBinding {

  @BindingAdapter("bind:fontName")
  public static void setFontName(TextView view, @NonNull String fontName) {
    String fontPath = "/fonts/" + fontName;

    Typeface typeface = Typeface.createFromAsset(view.getContext().getAssets(), fontPath);

    view.setTypeface(typeface);
  }
}

The error I'm getting:

Error:(8) No resource identifier found for attribute 'fontName' in package 'com.example.databindingproject'

Followed the tutorial from https://developer.android.com/tools/data-binding/guide.html . Any ideas of what I might be doing wrong?


Solution

  • You must use the data binding syntax. It should be:

    <TextView
          android:text="@string/hello_world"
          android:layout_width="wrap_content"
          app:fontName='@{"Roboto-Regular.ttf"}'
          android:layout_height="wrap_content"/>