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

Injecting Views

Kay-Uwe Janssen edited this page Oct 9, 2016 · 12 revisions

Since AndroidAnnotations 1.0

@ViewById

The @ViewById annotation indicates that an activity field should be bound with the corresponding View component from the layout. It is the same as calling the findViewById() method. The view id can be set in the annotation parameter, ie @ViewById(R.id.myTextView). If the view id is not set, the name of the field will be used. The field must not be private.

Usage example:

@EActivity
public class MyActivity extends Activity {

  // Injects R.id.myEditText
  @ViewById
  EditText myEditText;

  @ViewById(R.id.myTextView)
  TextView textView;
}

Method based injection

Since AndroidAnnotations 4.0.0

@EActivity
public class MyActivity extends Activity {

  @ViewById
  void setOneView(EditText myEditText){
    // do something with myEditText
  }
  
   void setMultipleBeans(@ViewById EditText myEditText, @ViewById(R.id.myTextView) TextView textView){
    // do something with myEditText and textView
  }

}

@AfterViews

The @AfterViews annotation indicates that a method should be called after the views binding has happened.

When onCreate() is called, @ViewById fields are not set yet. Therefore, you can use @AfterViews on methods to write code that depends on views.

Usage example:

@EActivity(R.layout.main)
public class MyActivity extends Activity {

    @ViewById
    TextView myTextView;

    @AfterViews
    void updateTextWithDate() {
        myTextView.setText("Date: " + new Date());
    }
[...]

You can annotate multiple methods with @AfterViews. Don't forget that you should not use any view field in onCreate():

@EActivity(R.layout.main)
public class MyActivity extends Activity {

    @ViewById
    TextView myTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // DON'T DO THIS !! It will throw a NullPointerException, myTextView is not set yet.
        // myTextView.setText("Date: " + new Date());
    }
[...]

Recall that injection is always made as soon as possible. Therefore, it's safe to use any field annotated, e.g., with @Extra or @InstanceState in @AfterViews methods as these tags don't require any view to be set (as @AfterViews do). Therefore you can safely assume that such fields will be already initialized with their intended values in methods annotated with @AfterViews:

@EActivity(R.layout.main)
public class MyActivity extends Activity {

    @ViewById
    TextView myTextView;

    @InstanceState
    Integer textPosition;

    @AfterViews
    void updateTextPosition() {
        myTextView.setSelection(textPosition); //Sets the cursor position of myTextView
    }
[...] // The remaining code must keep textPosition updated.

Warning

If the parent and child classes have @AfterViews, @AfterInject or @AfterExtras annotated methods with the same name, the generated code will be buggy. See issue #591 for more details.

Also, while there is a guaranteed order about when we call @AfterViews, -Inject or -Extras annotated methods, there is no guaranteed order for calling each of the methods with the same @AfterXXX annotation (see issue #810).

Details about when the methods with one of those annotations are called you can find here.

@ViewsById

Since AndroidAnnotations 3.1

This annotation is similar to @ViewById, but it injects a set of Views. It can be used on java.util.List or android.view.View subtype fields. The annotation value should be an array of R.id.* values. After injection the Views with the given IDs will be available in the List, but only the non-null ones as to avoid adding null checks to the code.

Usage example:

@EActivity
public class MyActivity extends Activity {

  @ViewsById({R.id.myTextView1, R.id.myOtherTextView})
  List<TextView> textViews;

  @AfterViews
  void updateTextWithDate() {
    for (TextView textView : textViews) {
      textView.setText("Date: " + new Date());
    }
  }
}

Method based injection

Since AndroidAnnotations 4.0.0

@EActivity
public class MyActivity extends Activity {

  @ViewsById({R.id.myTextView1, R.id.myOtherTextView})
  void setViews(List<TextView> textViews){
    // do something with textViews
  }
}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally