Android Annotations is 1 such useful framework which is used very widely in android apps development.If you are aware about Hibernate, Struts or spring framework then you must know about annotations. Annotations do not alter the flow of execution but can be used during compilation-execution stage.
Check the awesome apps built using Android Annotation.
Prerequisites: – You should have basic knowledge of Java-Android to understand this tutorial.
I have created sample calculator application using android annotation. You can download it from here : http://www.4shared.com/archive/1NTXX9sxba/AndRoidAnnotat.html
Steps Which I followed to create, Configure & Run Android Annotation project
Step 1 :- Create New Android project. To know how to do this, you can refer this tutorial : http://www.technonutty.com/2013/02/how-to-start-with-android-development.html
Step 2 :- Download Android Annotation library from here. https://github.com/excilys/androidannotations/wiki/Download
You will get 2 libs.
(1) Copy androidannotations-api-3.0.1.jar in libs folder & put it into class path of application.
(2) Put androidannotations-3.0.1.jar in any folder other than libs & don’t put it in class path of application.
Now, Right Click on Project –> Properties –> Java Compiler –> Annotation Processing –> Factory Path and include the androidannotations-3.0.1.jar in factory path.
Step 3 :- Now, it’s time to start writing annotation based coding. Layout files will be same as previous, annotations will be in java files which will reduce most of our coding efforts and make code cleaner.
Make sure that every android activity in manifest file should have ‘_’ appended because after compilation, annotation will append the ‘_’. So that both names remain in sync.
Here is our sample layout file.
Now, this is java code with annotations.
@Eactivity :- Specifies layout file associated with specific activity
@ViewById :- Associates layout components with Java variables so that it can referenced from Java code.
@Click :- associate the click functionality of layout components to java method
To associate, multiple components’s click to only 1 Java method, we can write something like this :
We discussed how to configure, create and run android annotation project. Now we will discuss about more annotations.
As for Activity annotation is @EActivity.
(1) Fragment :- @EFragment
(2) Service :- @EService
(3) BroadcastReceiver :- @EReceiver
(4) Application :- @EApplication
(5) ContentProvider :- @EProvider
(6) NotificationManager :- @SystemService
(7) Object :- @Bean
(8) MyRestClient :- @RestService
(9) Transaction support in Database Operations :- @Transactional
I have shared very few wth you all. To check all the annotations, check this link : https://github.com/excilys/androidannotations/wiki/AvailableAnnotations
To learn all the annotations practically, you can refer this : https://github.com/excilys/androidannotations/wiki/Cookbook
Another Small Example On OptionsMenu
To setup Options Menu. You have to create 1 menu layout file and specify it along with @OptionsMenu annotation. Inside activity, you can associate option menu items along with their respective methods.
Sample Option Menu in Android App :
public class MenuActivity extends Activity
void firstMenuItemCalled ()
//Add code for first menu item click
void secondMenuItemCalled ()
//Add code for second menu item click
Hope, you enjoyed learning Android Annotations framework.