If you have been in the mobile application development arena for quite some time and have been using android to code a lot of apps then you have come to the right place. The pain of writing the same code in each activity for basic things is being felt by a lot of android developers. Just imagine an android app/game with over 15-20 activities (I am talking about big apps), you can easily copy paste the code in the first version of your app but maintaining the same is a big headache as new features are added in the app.

How annotations simplify android app development

With annotations, we can reduce the amount of code it requires for doing simple things like binding onclick event listeners and using something like:

public class MyActivity extends Activity {
  Textview tv = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.main);
    tv = (TextView) findViewById(R.id.title);
    ....
  }
}

Compare the above with the following annotations based code:

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @ViewById(R.id.title)
  TextView tv;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    ....
  }
}

Steps to add annotations in Android project

Download the android annotations project from Androidannotations Github project
The downloaded bundle of annotations project comes with two jar files viz: androidannotations-X.X.X-api.jar and androidannotations-X.X.X.jar.

androidannotations-X.X.X-api.jar: The purpose of this JAR file is to provide the API’s so that your android project can use the annotations for injecting resources. This jar file has be present inside the libs folde of the android project.

androidannotations-X.X.X.jar: This jar file contains the annotations processor which shall analyze the annotations in your project and generate the boileplate code which you generall use (Extending from Activity, setting content view, finding widgets by using findViewById(…)). This jar file should be present inside the libs-ext folder of your android project.

Note: While the androidannotations-X.X.X-api.jar file shall be part of the apk exported by using Eclipse ADT plugin. The androidannotations-X.X.X.jar file is not exported along with the apk for your android project.

Configuring annotations using Maven

If you are using maven for dependency management in your android project then use the following snippet in pom.xml to add the required jars into your project:

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<android.version>4.1.1.4</android.version>
		<android.platform>16</android.platform>
		<androidannotations.version>2.7.1</androidannotations.version>
		<java.version>1.6</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.google.android</groupId>
			<artifactId>android</artifactId>
			<version>${android.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.googlecode.androidannotations</groupId>
			<artifactId>androidannotations</artifactId>
			<version>${androidannotations.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.googlecode.androidannotations</groupId>
			<artifactId>androidannotations-api</artifactId>
			<version>${androidannotations.version}</version>
		</dependency>
	</dependencies>

Example Activity code

A basic Android activity which uses the annotations is listed below:

@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);
    }

    // [...]
}

The above source code has been taken from http://www.sapandiwakar.in/technical/building-android-applications-using-android-annotations/

Adding entry in AndroidManifest.xml

After creating the above activity in your Eclipse project, add the following entry in the AndroidManifest.xml file:

Note the extra “_” character in the above line.

Other annotations

Apart from the above basic example of injecting resources using android annotations project, there a lot of advanced annotations available like:

  • @AfterViews for executing some logic after the views (TextView, ImageView) have been injected
  • @OptionsMenu(R.menu.my_menu) for injeting the action bar menu to your activity.
  • @Background for executing in a method in background thread
  • @Fullscreen to hide the notifications bar and allow your application to take up whole of the mobile device space
  • @SystemService to inject a system service. The awesome feature is that if the variable which is annotated with @SystemService will be injected with the service depending upon the type of variable. For e.g. if the variable is of type ClipboardManager then the clipboard service is injected.

The library offers the following features for any android app:
Dependency injection
Simplified threading model
Event binding
REST client

A complete list of annotations which can be used in the project are:

https://github.com/excilys/androidannotations/wiki/Cookbook

Important points

1) The android project should be compiled using JDK 1.5 or JDK 1.6
2) Annotations processing should have been enabled “Project Properties” > “Java Compiler” > “Annotation Processing”
3) Android annotations project is also commonly referred to as AA. So next time don’t get surprised if some android developer says that he used AA 2.7 in his application/game.

Android annotations video tutorial

You can also go through the following video tutorial to get started with android annotations library. The video description contains link to the project created in the tutorial.

Sample skeleton project

In order to get started with android app development, use the following Hello World project taken from the android annotations library:
HelloWorldEclipse

References

Android annotations home page
Using android annotations in IntelliJ IDEA

Related Posts

Android app development using android annotations admin Android
If you have been in the mobile application development arena for quite some time and have been using android to code a lot of apps then you have come to the right place. The pain of writing the same code in each activity for basic things is being felt...
If you have been in the mobile application development arena for quite some time and have been using android to code a lot of apps then you have come to the right place. The pain of writing the same code in each activity for basic things is being felt by a lot of android developers. Just imagine an android app/game with over 15-20 activities (I am talking about big apps), you can easily copy paste the code in the first version of your app but maintaining the same is a big headache as new features are added in the app. https://www.youtube.com/watch?v=BRXXKL9cGfU <h2>How annotations simplify android app development</h2> With annotations, we can reduce the amount of code it requires for doing simple things like binding onclick event listeners and using something like: 1 Compare the above with the following annotations based code: 1 <h2>Steps to add annotations in Android project</h2> Download the android annotations project from <a href="https://github.com/excilys/androidannotations">Androidannotations Github project</a> The downloaded bundle of annotations project comes with two jar files viz: androidannotations-X.X.X-api.jar and androidannotations-X.X.X.jar. <strong>androidannotations-X.X.X-api.jar:</strong> The purpose of this JAR file is to provide the API's so that your android project can use the annotations for injecting resources. This jar file has be present inside the libs folde of the android project. <strong>androidannotations-X.X.X.jar</strong>: This jar file contains the annotations processor which shall analyze the annotations in your project and generate the boileplate code which you generall use (Extending from Activity, setting content view, finding widgets by using findViewById(...)). This jar file should be present inside the libs-ext folder of your android project. <strong>Note:</strong> While the androidannotations-X.X.X-api.jar file shall be part of the apk exported by using Eclipse ADT plugin. The androidannotations-X.X.X.jar file is not exported along with the apk for your android project. <h2>Configuring annotations using Maven</h2> If you are using maven for dependency management in your android project then use the following snippet in pom.xml to add the required jars into your project: 1 <h2>Example Activity code</h2> A basic Android activity which uses the annotations is listed below: 1 The above source code has been taken from http://www.sapandiwakar.in/technical/building-android-applications-using-android-annotations/ <h2>Adding entry in AndroidManifest.xml</h2> After creating the above activity in your Eclipse project, add the following entry in the AndroidManifest.xml file: <activity android:name=".ActivityWithExtra_" /> Note the extra "_" character in the above line. <h2>Other annotations</h2> Apart from the above basic example of injecting resources using android annotations project, there a lot of advanced annotations available like: <ul> <li>@AfterViews for executing some logic after the views (TextView, ImageView) have been injected</li> <li>@OptionsMenu(R.menu.my_menu) for injeting the action bar menu to your activity.</li> <li>@Background for executing in a method in background thread</li> <li>@Fullscreen to hide the notifications bar and allow your application to take up whole of the mobile device space</li> <li>@SystemService to inject a system service. The awesome feature is that if the variable which is annotated with @SystemService will be injected with the service depending upon the type of variable. For e.g. if the variable is of type ClipboardManager then the clipboard service is injected.</li> </ul> The library offers the following features for any android app: <strong>Dependency injection Simplified threading model Event binding REST client</strong> A complete list of annotations which can be used in the project are: https://github.com/excilys/androidannotations/wiki/Cookbook <h2>Important points</h2> 1) The android project should be compiled using JDK 1.5 or JDK 1.6 2) Annotations processing should have been enabled "Project Properties" > "Java Compiler" > "Annotation Processing" 3) Android annotations project is also commonly referred to as AA. So next time don't get surprised if some android developer says that he used AA 2.7 in his application/game. <h2>Android annotations video tutorial</h2> You can also go through the following video tutorial to get started with android annotations library. The video description contains link to the project created in the tutorial. <iframe width="420" height="315" src="//www.youtube.com/embed/BRXXKL9cGfU" frameborder="0" allowfullscreen></iframe> <h2>Sample skeleton project</h2> In order to get started with android app development, use the following Hello World project taken from the android annotations library: <a href="http://www.javaexperience.com/wp-content/uploads/2013/08/HelloWorldEclipse.zip">HelloWorldEclipse</a> <h2>References</h2> <a href="http://androidannotations.org/">Android annotations home page</a> <a href="http://www.jayway.com/2012/08/31/making-androidannotations-work-with-intellij-idea">Using android annotations in IntelliJ IDEA</a>
Android textbox, edittext and textarea example
Android Secrets for Developers
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments