43

This has to be the most basic of question but after a full day of reading tutorials and the documentation here I can't seem to understand where to put these methods. None of the guides mention where this thing is placed it just mentions to use the annotation on a static method. What static method? Any static method at all regardless of class? What is a good practice? do I create a CustomBinding class to host all these static methods?

So long as I have the method has a BindingAdapter annotation and the first parameter is a View, it will work?

I assume if the first parameter is of type View I can place the binding attribute on any type of views and it will trigger the method? So if I have specific view say EditText, does this mean the method is only called when the attribute is found in an EditText view in the layout file?

chaser
  • 3,107
  • 4
  • 29
  • 34
  • 1
    see how it is done with already existing adapters, like [TextViewBindingAdapter](https://android.googlesource.com/platform/frameworks/data-binding/+/android-6.0.0_r7/extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java) and [parent folder](https://android.googlesource.com/platform/frameworks/data-binding/+/android-6.0.0_r7/extensions/baseAdapters/src/main/java/android/databinding/adapters) – pskink Nov 28 '16 at 14:15

2 Answers2

31

After navigating through the internet I've finally found some info from one of the developers themselves. I wish they would have been more clear on the basics in the documentation.

Quote:

Binding adapters are annotated methods in any class that are used to do just this. Typically, you’d organize your adapters into [-a] classes based on the target View type.

This obviously means that at compile time all methods in any class with the annotation BindingAdapter will generate the BindingAdapter.

chaser
  • 3,107
  • 4
  • 29
  • 34
  • Thanks a lot! You saved hours of my time – shift66 Mar 25 '18 at 13:39
  • 1
    or you can go [right here about defining the recommended ways](https://medium.com/@thinkpanda_75045/defining-android-binding-adapter-in-kotlin-b08e82116704) (if you using kotlin) to optimize `@BindingAdapter` method – mochadwi Mar 01 '20 at 04:00
5

You place it in your model class.

Example:

XML:

 <data>

    <variable
        name="item"
        type="com.yourpackage.Model"/>
      </data>
         ......

           <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@{item.resId}"/>

Model:

public class Model {

    @DrawableRes
    private final int resId;

    public Model(int resId) {
        this.resId = resId;
    }

    public int getResId() {
        return resId;
    }

    @BindingAdapter ("android:src")
    public static void setImageResource(ImageView imageView, int resource){
        imageView.setImageResource(resource);
    }
}
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Manza
  • 3,427
  • 4
  • 36
  • 57
  • Please explain whether this is the only way to do it or is this your way? There are some BindingAdapters that are grouped in a class, eg https://android.googlesource.com/platform/frameworks/data-binding/+/android-6.0.0_r7/extensions/baseAdapters/src/main/java/android/databinding/adapters/TextViewBindingAdapter.java – chaser Nov 29 '16 at 10:32
  • 10
    I don't think model class is not a good place to put this BindingAdapter because it is not meant to couple with the model. For example, I can't do two `@BindingAdapter ("android:src")` - one to scale the image to 200x200 and another to scale image to 400x400. I think it might be better to put them in a class specifically for binding (eg. `BindingAdapterUtils`). – Sam Jul 02 '17 at 23:48
  • @Sam do you have any idea while using Binding Adapter without custom views? I tried to bring to other file but don't know how to put it in tag – Harry T. Jul 03 '17 at 06:54
  • 1
    @TruongHieu, you do not need a custom view. You can use it for anything. Just create a new file (eg. BindingUtils) and place a **static method** there. Remember to declare `@BindingAdapter` (eg. `@BindingAdapter ("android:src")`). – Sam Jul 03 '17 at 09:55
  • Sure I tried it with custom attribute successfully. But why I didnt see inspection of that custom attribute? @Sam – Harry T. Jul 03 '17 at 10:33
  • Hi Truong, I don't quite understand your question. Perhaps open a new question thread with more detail explanation :) – Sam Jul 03 '17 at 10:36
  • 1
    another example: https://github.com/google/iosched/blob/89df01ebc19d9a46495baac4690c2ebfa74946dc/mobile/src/main/java/com/google/samples/apps/iosched/ui/search/SearchBindingAdapters.kt – Saba Nov 05 '19 at 19:47