Speed up your productivity on Android : Android Annotations

Introduction

As an Android developer, you might sometimes ask yourself: “Why Android, which is a very cool platform, has so many lack in productivity when you have to develop?”. Of course Android is easy to use and offers a lot of really great stuff for developers. But yes, sometimes, there is a lot of boilerplate and you lose a lot of time. Fortunately, some libraries make our developer life easier! Here’s the first article of a series about how to boost your productivity with awesome libraries. And the first one is Android Annotations !

 

What’s Android annotations?

Android annotations is a great open-source (and French) library to speed up your code writing (well, that’s the goal of the article, right?). How? It just simplifies a lot of stuff you don’t care about. For example, one of the most boilerplate you do as an Android developer is to get UI elements reference in your code.

TextView myTextView = (TextView) findViewById(R.id.myTextView)

Okay, that’s easy, but it takes time. If you have one or two elements, that’s okay. If you have hundreds of them, you have a real lack of productivity. Moreover, if you want to reference your element as attribute of your class, you will have to split the code in two places (one to declare the attribute, the other to get it from the view). But here comes Android Annotations!

@ViewById
TextView myTextView;

 

Much easier right ? Here is the strength of the library, you use annotations everywhere and your development becomes suddenly so productive! But it’s just the beginning!

Are you convinced? Then go download the library right now on Github (and fork it)

http://androidannotations.org/

Initialize your Activity

Before using all the awesome stuff of the library, you have some steps to do. First of all, you have to add the library to your project. This step is very well documented on the Github project Github. Once it’s done, you have to annotate your activity with the @EActivity annotation. Actually, this annotation will generate a subclass of your activity, which will be used in the application. Once your activity is annotated, go to your manifest, and replace the name of your Activity by the generated subclass name, which is the same, but with an underscore at the end. For example, if your activity was declared this way:

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

Now it’s:

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

Once done, you can use all the features of Android Annotations.

 

Use views with Android Annotations

The @ViewById is not the only helpful annotations included in the library. A lot of stuff exists. But here is an important thing to know: don’t use your views directly in the onCreate method. Why? Because all your elements will be null and so you will think “What? This guy on the blog is a liar. It doesn’t work at all!”. Of course it works! But all your elements aren’t retrieved by the library yet. Instead, you should use them on a special method, which is annotated with @AfterViews:

@ViewById
protected TextView myTextView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     
     //Here you have to NOT use annotated elements
}

@AfterViews
protected void useElements()
{
     //Here you can use your annotated elements
}

 

In this method, you can safely use your elements. By the way, you are not limited to UI elements. You can also get Animations or services with annotations!

@AnimationRes
Animation fadeIn;
	
@SystemService
ClipboardManager clipboardManager;

 

Use events

Okay, now you can get your elements with annotations. But it’s still not the end! The library allows you to handle events like click on your elements, without modifying the view! You just have to use the @Click annotation:

@Click({R.id.button1, R.id.button2})
protected void buttonClick() 
{
     //Do what you want here
}

It’s not a top feature in my opinion but it can be quite useful!

Background and UI threads

Thanks to JDK, using threads is easy. But anyway, you still have to use an AsyncTask, or use callbacks to call UI thread from a background thread. Not really convenient. But once again, Android Annotations offers two awesome annotations to accelerate our development:

You want to put a method in a background thread? No problem, just use @Background annotation!

@Background
protected void taskInBackground(String stuff) 
{
     //All the code of the method will not be executed in UI thread
}

Yes. That’s all. Your method is in a background thread. Just one thing to know: your method has to not be private.

And what about UI thread? Same way, use @UIThread annotation:

@UiThread
protected void taskInBackgroundCompleted(String result)
{
     Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}

Now you are all done. It is more convenient than usual threads right?

 

Conclusion

This article is just an introduction, there is a lot more to discover on this library, like the @Rest annotation.  I hope it has given you some ideas and convince you to use Android Annotations. Don’t lose time with boilerplate stuff and spend time on the core of your app, not on findViewById method.

 

 

 

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>