How to use android data binding to bind an interface?

I also can not find the way to make it work with interface but I think you can use Abstract class like this.

public abstract class IXObject extends BaseObservable {
    public abstract void setName(String name);

    @Bindable
    public abstract String getName();
}

.

public class X1Object extends IXObject {
    private String name;

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }
}

.

final IXObject xobj = new X1Object();
xobj.setName("nguyen");
binding.setXObject(xobj);

When you declare a variable in your layout file you must take it literally. interfaces are not objects. I know Java supports generic inline objects where you can declare an interface as if it were an object, but in general they are not objects.

So when you say to the layout file to use a variable of the interface type, it really can't do much as it is not an object.

One thing you will notice and hate about android data binding is that it cannot infer types. So if you are trying to set a variable using an implementing class, but hoped to do it with an interface you will not have any luck.

You need to specify the class that will be used, not the interface that those classes inherit from. As the other answer pointed out, you can utilize a base class if your base class has the necessary methods.

Also quick note you can simply put the binding on the private member variable and it will still go through the getter and setters automatically.

The only other thing you could do is create an adapter that takes the interface or your interface or even Any with a cast, and sets the name for you. Which requires one more adapter in your already growing adapters class, but at least they are straight forward to do.