The inflate method for my binding is not found (using Android, Data Binding.)
Something is not completing in Android Studio because you haven't actually implemented data binding.
Once you add a variable to your layout's data
element, the inflate
method will be found as you expect. That said, you're really not getting the benefit of databinding by setting the value of the text field directly through the binding. You should instead be setting a View Model in your binding, and then let the binding update the views accordingly. For example:
create a View Model:
public class MyViewModel {
public final ObservableField<String> name;
public MyViewModel(String name) {
this.name = new ObservableField<>(name);
}
}
and use it in your layout
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="model" type="com.victoriaschocolates.conceirge.MyViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{model.name}"
android:padding="10dp"
android:background="#000"
android:textColor="#fff"
android:layout_gravity="center"
/>
</FrameLayout>
</layout>
(note the variable
declared in the data
element, and how it is referenced in the TextView's text
attribute)
then bind the two in your custom view:
public class MyCustomView extends FrameLayout {
public MyCustomView(Context context) {
this(context, null, 0);
}
public MyCustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MyCustomBinding binding = MyCustomBinding.inflate(inflater, this, true);
MyViewModel model = new MyViewModel("Whatever");
binding.setModel(model);
}
}
Of course, it would probably be better still to have the data passed in through a setter in the custom view class, or even passed in from the container view (see http://developer.android.com/tools/data-binding/guide.html#includes)
You can try setting a custom class name to your Binding Layout, and referencing it in your custom view to make it clear which layout you using:
<layout>
<data class="MyCustomBinding">
</data>
</layout>
If this does not work, use DataBindingUtil
instead of MyCustomBinding
, which returns the base DataBinding class and has a inflate()
method:
MyCustomBinding binding = DataBindingUtil.inflate(inflater, this, true);
From the docs:
Sometimes the binding cannot be known in advance. In such cases, the binding can be created using the DataBindingUtil class:
ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId,
parent, attachToParent);
ViewDataBinding binding = DataBindingUtil.bindTo(viewRoot, layoutId);