Sapan Diwakar

Software developer

Follow me on Twitter Check out my code on GitHub View some of my designs on Dribbble Take a look at my Linked In profile

Building Android applications using Android Annotations

If you have previously written applications for Android, you already know that there is a lot of code that has to be written over and over again. It not only takes more time to write the initial version of the code, it makes it so much more harder to manage and maintain. Android Annotations facilitates the writing and maintenance of Android applications by simplifying this process for you and taking out most of the repetitive code. Here's a small example of how simple it makes to do common things (code sample taken from AndroidAnnotations Wiki):

@EActivity(R.layout.translate) // Sets content view to R.layout.translate
public class TranslateActivity extends Activity {  
    @ViewById // Injects R.id.textInput
    EditText textInput;

    @ViewById(R.id.myTextView) // Injects R.id.myTextView
    TextView result;

    @AnimationRes // Injects android.R.anim.fade_in
    Animation fadeIn;

    @Click // When R.id.doTranslate button is clicked 
    void doTranslate() {
        translateInBackground(textInput.getText().toString());
    }

    @Background // Executed in a background thread
    void translateInBackground(String textToTranslate) {
         String translatedText = callGoogleTranslate(textToTranslate);
         showResult(translatedText);
    }

    @UiThread // Executed in the ui thread
    void showResult(String translatedText) {
         result.setText(translatedText);
         result.startAnimation(fadeIn);
    }

    // [...]
}

Including Android Annotaions in a project is very easy. We just need two JARs, one for the Android Annotations API and another for processing the annotations at compile time. Begin by downloading these JARs from here. We now need to add the JARs and enable annotation processing in eclipse.

  • Put androidannotations-X.X.X-api.jar in the libs folder.
  • Put androidannotations-X.X.X.jar in a different folder, such as ext-libs.
  • androidannotations-X.X.X.jar must not go in the libs folder
  • In project properties, set Compiler Compliance level for Java Compiler to 1.6
  • In project properties > Java Compiler > Annotation Processing, make sure that Enable Annotation processing is checked.
  • Add androidannotations-X.X.X.jar in Java Compiler > Annotation Processing > Factory Path

I will list some of the most important features that I have found useful while working with Android Annotations. To create an activity, we just need to annotate a class that extends Activity with @EActivity with a parameter for the content view for the activity.

@EActivity(R.layout.main)
public class MyActivity extends Activity {  
    @ViewById(R.id.myTextView)
    TextView myTextView;

    @AfterViews
    void afterViews() {
        // TODO: Do something after the views are injected
        myTextView.setText("Hello World!");
    }

    @Click(R.id.myButton)
    void onMyButtonClick() {
        // TODO: My button clicked. Do something
    }

    @ItemClick(R.id.myListView)
    void onMyListViewItemClicked(int position) {
        // TODO: Item at position clicked in my list view. 
    }
}

You will notice in the example that we didn't have to set the content view for the activity in onCreate() or to manually set the onClick or the onItemClick listeners for the myButton and myListView or to find the views by id. Android Annotations takes care of most of the things and makes the code more readable and easier to write and maintain. And this is just the beginning. It handles a lot more things. It does this by generating a class that extends your annotated class. For this reason, while registering our Activity in the manifest file, we must register the file that Android Annotations generates and not the original annotated file that we write. This is very important because otherwise, we wouldn't see anything as we didn't actually set the content view in the annotated file. Instead of registering MyActivity, register MyActivity_ in AndroidManifest.xml.

<activity android:name=".MyActivity_" />  

You should always remember to refer to MyActivity_ instead of MyActivity everywhere else in the code.

Another heavily used feature in Android is the support for Fragments. Android Annotations supports fragments too. All you need to do is to annotate a class that extends Fragment with @EFragment and you can use all the annotations that you were able to use above with Activities. To inject a fragment in an activity, you can use the @FragmentById or @FragmentByTag tag. Make sure that you use the generated class name for the fragment (with the _) rather than the original one whenever you refer to the fragment. Android Annotations doesn't create the fragment for you. So also make sure that the fragment should exist either in the layout or created programatically in onCreate or some other method.

For a full list of annotations supported in Android Annotations, refer to the Cookbook.