• 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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?

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"/>
  • 72
Reply Report
      • 1
    • 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?
    • 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?
      • 1
    • Yep - the data binding variables work fine. Tried this in a new project, with just the @BindingAdapter and it gives the same error.
      • 1
    • You must use a data binding layout. The root tag must be or else the data binding system won't know that it should parse your layout file.
      • 2
    • You must inflate with DataBinding. In onCreate, use DataBindingUtil.setContentView(this, R.layout.activity_main) instead of setContentView(R.layout.activity_main)

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
Reply Report