marius bardan
marius bardan

Reputation: 5122

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?

Upvotes: 38

Views: 11923

Answers (5)

Abbas Jafari
Abbas Jafari

Reputation: 1641

I know it is too late I did all the answers but it didn't work for me, and when I added this code:

dataBinding {
    enabled = true
}

in build.gradle in the app-level folder, inside the android tag like below:

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.development.allanproject"
        minSdkVersion 22
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    dataBinding {
        enabled = true
    }
}

My issue was fixed.

Upvotes: 0

Admire Masaga
Admire Masaga

Reputation: 61

As shown hereTo configure your app to use data binding, add the dataBinding element to your build.gradle file in the app module, as shown in the following example:

android {
    ...
    dataBinding {
        enabled = true
    }
}

Upvotes: 0

If you want to pass dp/px value to @BindingAdapter - remember to wrap it with a binding syntax i.e. "@{ }"

You need to use dimens.xml to pass the value:

app:compoundDrawableSize="@{@dimen/icon_size}"

Accompanying binding adapter enabling you to set copound drawable size for a TextView would look like the following:

/**
 * Binding adapter to set the size of TextView's compound drawable
 *
 * Usage:
 * <TextView   [...]
 *       android:drawableLeft="@{@drawable/ic_myicon}"
 *       app:compoundDrawableSize="@{@dimen/icon_size}"
 *  />
 */
@BindingAdapter("bind:compoundDrawableSize", "android:drawableLeft", "android:drawableStart",
        "android:drawableRight", "android:drawableEnd", requireAll = false)
fun TextView.setCompoundDrawableSize(px: Float, @IntegerRes drawableLeft: Drawable?, @IntegerRes drawableStart: Drawable?,
                                     @IntegerRes drawableRight: Drawable?, @IntegerRes drawableEnd: Drawable?) {
    val compoundDrawableLeft = drawableLeft ?: drawableStart
    val compoundDrawableRight = drawableRight ?: drawableEnd
    compoundDrawableLeft?.setBounds(0, 0, px.toInt(), px.toInt())
    compoundDrawableRight?.setBounds(0, 0, px.toInt(), px.toInt())
    this.setCompoundDrawables(compoundDrawableLeft, null, compoundDrawableRight, null)
}

Upvotes: 0

Wolfgang
Wolfgang

Reputation: 3540

This same error can also happen if you forget the closing curly brace:

android:text="@{viewModel.foo"

Upvotes: 17

George Mount
George Mount

Reputation: 20926

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

Upvotes: 76

Related Questions