38

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?

6
  • Are you trying to add a custom font resource for your app to use? Sep 4, 2015 at 12:51
  • Where and how setFontName is called? Sep 4, 2015 at 12:59
  • 1
    Not important right now if I'm trying a custom font or not. Just wanting to define a custom attribute via the android data binding mechanism. @Angudroid - check the link from the post - the doc says the method will be called by the system. Sep 4, 2015 at 13:02
  • @marius, okay, so when you breakpoint at "String fontPath" what is the fontName value ? Sep 4, 2015 at 14:24
  • it's a compile time error. so no breakpoints :) Sep 4, 2015 at 14:32

5 Answers 5

76

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"/>
11
  • Now I'm getting another error as well: Error:(24, 21) No resource type specified (at 'fontName' with value '@{"Roboto-Regular.ttf"}'). Is there any (github) example project showcasing this? Sep 6, 2015 at 17:13
  • It appears as if your BindingAdapter method is not being found. I'm not sure why that would be. Does other data binding work for you? Sep 8, 2015 at 14:35
  • Yep - the data binding variables work fine. Tried this in a new project, with just the @BindingAdapter and it gives the same error. Sep 9, 2015 at 7:25
  • 8
    You must use a data binding layout. The root tag must be <layout> or else the data binding system won't know that it should parse your layout file. Sep 11, 2015 at 18:04
  • 4
    You must inflate with DataBinding. In onCreate, use DataBindingUtil.setContentView(this, R.layout.activity_main) instead of setContentView(R.layout.activity_main) Sep 14, 2015 at 23:17
16

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

android:text="@{viewModel.foo"
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)
}
0

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
    }
}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.