Skip to content
pyricau edited this page Jan 10, 2012 · 4 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:

@ViewById
EditText myEditText;

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

Equivalent boilerplate code:

EditText myEditText;

TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
    [...]
    myEditText = (EditText) findViewById(R.id.myEditText);
    textView = (TextView) findViewById(R.id.myTextView);
}

@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());
    }
[...]

Equivalent boilerplate code:

public class MyActivity extends Activity {

    TextView myTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myTextView = (TextView) findViewById(R.id.myTextView);
        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());
    }
[...]

@BeforeCreate

Any method annotated with @BeforeCreate will be called before your onCreate() method is called. The method may have a Bundle parameter (which will be the onCreate savedInstanceState parameter).

This can be useful to separate code logics, or if you don't like the the look of the super.onCreate() code, or if you don't need the savedInstanceState param.

Usage example:

@EActivity
public class MyActivity extends Activity {

    @BeforeCreate
    void init() {
        // do some init code
    }

[...]

Equivalent boilerplate code:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // do some init code
        super.onCreate(savedInstanceState);
    }
[...]

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Clone this wiki locally