I am trying to define an attribute for any view using the Data Binding Library, as explained in this Android Developers post.
To do so, the post says one first needs a layout with an enclosing <layout>
tag:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:attribute='@{"name"}'/>
</LinearLayout>
</layout>
At this point, the layout caused a ClassNotFoundException
when inflated. The only way I found to get rid of it was to add a <data></data>
node, even if it was absent from the Android Developers post:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data></data>
...
</layout>
(The post does not mention it, but I had to enable dataBinding
in my build.gradle
as recommended in the Guide before I could build.)
The post then explains how to write a BindingAdapter
method to process the attribute:
import android.databinding.BindingAdapter;
import android.util.Log;
import android.view.View;
public class AttributesBindingAdapter {
@BindingAdapter("bind:attribute")
public static void bindAttribute(View view, String attributeName){
Log.e("PLN", attributeName);
}
}
However, the bindAttribute
method is never called. I do see the generated code for the layout in my build folder, but nothing else happens.