You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@EActivitypublicclassMyActivityextendsActivity {
@ViewByIdvoidsetOneView(EditTextmyEditText){
// do something with myEditText
}
voidsetMultipleBeans(@ViewByIdEditTextmyEditText, @ViewById(R.id.myTextView) TextViewtextView){
// 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.
You can annotate multiple methods with @AfterViews. Don't forget that you should not use any view field in onCreate():
@EActivity(R.layout.main)
publicclassMyActivityextendsActivity {
@ViewByIdTextViewmyTextView;
@OverridepublicvoidonCreate(BundlesavedInstanceState) {
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)
publicclassMyActivityextendsActivity {
@ViewByIdTextViewmyTextView;
@InstanceStateIntegertextPosition;
@AfterViewsvoidupdateTextPosition() {
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.