Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Data binding support

Kay-Uwe Janssen edited this page Dec 7, 2018 · 4 revisions

Since AndroidAnnotations 4.4

You can use data bound layouts in @EActivity, @EFragment and @EViewGroup annotated classes. To achieve this, you must annotated these classes with the additional @DataBound annotation.

You have to access the data binding object to bind your data to the UI. To inject the binding object, annotate a non-private field with @BindingObject. The type of the field must extend ViewDataBinding. You can first access this field in an @AfterViews annotated method.

Example:

@DataBound
@EActivity(R.layout.main_activity)
public class MainActivity extends Activity {

    @BindingObject
    MainActivityBinding binding;

    @AfterViews
    void bindData() {
        binding.setTitle("Your title"); // example binding
    }
}

Method based injection

You can use method injection as well for @BindingObject.

@DataBound
@EActivity(R.layout.main_activity)
public class MainActivity extends Activity {

    @BindingObject
    void injectBinding(MainActivityBinding binding) {
        // assign binding
    }

    void injectBinding2(@BindingObject MainActivityBinding binding) {
        // assign binding
    }

}

Errors with generated views and binding adapters

Since data binding V2, you cannot pass generated View classes in your binding adapters, but you still have to use your generated views to let AndroidAnnotations work.

There is a workaround: you can have a parent Activity which inflates the generated views automatically using a little reflection. Subclass that Activity, and then you can reference the normal (not generated) Views in your XML layouts and binding adapters.

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally