android annotations n.
Skip this Video
Loading SlideShow in 5 Seconds..
Android Annotations PowerPoint Presentation
Download Presentation
Android Annotations

Loading in 2 Seconds...

play fullscreen
1 / 38

Android Annotations - PowerPoint PPT Presentation

  • Uploaded on

Android Annotations. Dale King. Goal. Give an overview of AndroidAnnotations to whet your appetite I’ll touch on probably 90% of the annotations My goal is not to make you an expert on AndroidAnnotations , but just make you want to go try it and learn more. What are AndroidAnnotations?.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Android Annotations' - jayme

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
  • Give an overview of AndroidAnnotations to whet your appetite
  • I’ll touch on probably 90% of the annotations
  • My goal is not to make you an expert on AndroidAnnotations, but just make you want to go try it and learn more.
what are androidannotations
What are AndroidAnnotations?
  • AndroidAnnotations is an Open Source annotation processing library that speeds up Android development
  • It takes care of the plumbing, and lets you concentrate on what's really important.
  • By simplifying your code, it facilitates its maintenance.

"The ratio of time spent reading [code] versus writing is well over 10 to 1... making it easy to read makes it easier to write." - Robert C. Martin

what is annotation processing
What is Annotation Processing?
  • Annotation Processing is a feature of the Java compiler that lets you write a library to process source code annotations at compile time
  • Runs your annotation processing code in the compiler and can generate new source code files that will also be compiled
  • There is no way with public API to modify existing classes
how does it work
How does it work?
  • AndroidAnnotations is a Java Annotation Processing Tool that generates Android specific source code based on annotations
  • It enhances a class by subclassing it with a generated class that adds and overrides methods in the annotated class
  • The generated class is named by appending an underscore to the name of the annotated class
how do you use it
How do you use it?
  • APT is built into Sun javac so all you have to do is include Android Annotations jar in compile time class path
  • In Eclipse it needs to be enabled for the project
    • There is a bug in the Eclipse compiler that causes spurious errors if you import an APT generated class. So either access from same package or fully qualify
how do you use it1
How do you use it?
  • Everywhere in the project that you refer to an enhanced class you append an underscore to the class name:
    • In the Android Manifest
    • In layout resources
    • In the code
  • The git repo has examples on setting up with maven + eclipse and gradle
enhanced component annotations
Enhanced Component Annotations
  • Code generation begins with a class that is annotated with one of the enhanced component annotations (which start with E)
  • @EActivity
  • @EApplication
  • @EBean
  • @EFragment
  • @EProvider
  • @EReceiver
  • @EService
  • @EView
  • @EViewGroup
layout injection with @eactivity and @efragment
Layout Injection With@EActivity and @EFragment

public class MyListActivity extends ListActivity {


public void onCreate(Bundle savedInstanceState){





public class MyListActivity extends ListActivity {

fluent intent building interface for eactivity and eservice
Fluent Intent Building Interface for EActivity and EService

Intent intent = new Intent(myContext,





view injections
View Injections
  • For components with associated views you can have views automatically injected into fields


publicclassMyActivityextends Activity {

// Injects




resource injections
Resource Injections
  • Can have resources automatically injected into fields. 16 different annotations based on type:


publicclassMyActivityextends Activity {


String myHelloString;

@IntegerResint offset;

@ eapplication and @app
@EApplication and @App
  • EApplication adds no new functionality to annotated application but let’s you automatically inject the application into any other enhanced component using @App annotation:


publicclassMyActivityextendsActivity {



@ ebean and @bean
@EBean and @Bean
  • EBean lets you enable enhancement for your own custom classes. Class must have one constructor that takes no parameters or a Context parameter.
  • You can automatically inject new instances of a bean into any other enhanced component using @Bean annotation:
  • Optionally EBean can be declared as a singleton
injecting beans
Injecting Beans


publicclassMyActivityextends Activity {





  • @Extra lets you inject extra values from an intent into an EActivity


publicclassMyActivityextends Activity {


String myMessage;

@Extra Date myDate = new Date();

fragment injection
Fragment Injection
  • Lets you inject fragments by ID or by tag
  • Does not create them, must already be created (e.g. in the layout)






fragment argument injection
Fragment Argument Injection
  • Lets you inject arguments to a fragment into an enhanced fragment:


publicclassMyFragmentextends Fragment {


String myMessage;

@FragmentArg String anotherStringArgument;

system service injection
System Service Injection
  • Simplifies accessing system services from an activity:





publicclassMyActivityextends Activity {



@ afterviews
  • Method annotation for methods that are called after views are injected:


publicclassMyActivityextends Activity {



myTextView.setText("Date: " +new Date());


@ afterinject
  • Method annotation for methods to be called when injection is complete for @Ebean


@Bean MyOtherClassdependency;


// dependency is null


@AfterInjectpublicvoiddoSomething (){

// dependency is set here


event binding
Event Binding
  • Method annotations for methods that are automatically called in response to UI events
















event binding examples
Event Binding Examples




voidanotherButton(){... }


voidyetAnotherButton(View clickedView){...


@background annotation
@Background Annotation
  • Method annotation that causes method to be executed on a background thread


someBackgroundWork("hello", 42);


@Background voidsomeBackgroundWork(String aParam,longanotherParam)


// …


@background parameters
@Background parameters
  • @Background(id="cancelable_task")
    • Allows job to be cancelled based on id
  • @Background(serial = "test")
    • All background tasks with same serial value will be executed sequentially
  • @Background(delay=2000)
    • Delay before starting task
@ uithread
  • Method annotation that causes method to run on UI Thread (supports delay parameter):


doInUiThread("hello", 42);



voiddoInUiThread(String aParam,longanotherParam){

// ...


eactivity modification annotations
EActivity Modification annotations
  • Annotations to add to an EActivity to modify its behavior
  • @NoTitle
    • Shows activity with no title
  • @FullScreen
    • Requests full screen
  • @CustomTitle(R.layout.custom_title)
    • Use custom resource for title
shared preference annotations
Shared Preference Annotations
  • Annotations to create a type-safe shared preference interface:



@DefaultString("John") String name();

@DefaultInt(42) intage();



injecting shared preference
Injecting Shared Preference
  • The @Pref annotation will inject a shared preference instance:


publicclassMyActivityextends Activity {


MyPrefs_ myPrefs;

// ...


modifying shared preferences
Modifying Shared preferences

// Simple edit"John");

// Batch edit



// Preference clearing:


reading shared preferences
Reading Shared Preferences

// Check if a value exists:


// Reading a value


// Reading a value and providing a default

longnow =System.currentTimeMillis();


options menu annotations
Options Menu annotations



publicclassMyActivityextends Activity { @OptionMenuItem




// …


options menu continued
Options Menu continued



// The "Selected" keyword is optional




@OptionsItemvoidmenu_add(MenuItem item){

// You can add a MenuItemparameter


@ instancestate
  • Field annotation to cause field to be saved and restored in instance state automatically


publicclassMyActivityextends Activity {





@ onactivityresult
  • Method annotation to connect methods as callbacks for startActivityForResult


voidonResult(intresultCode, Intent data){}@OnActivityResult(REQUEST_CODE)

voidonResult(intresultCode){} @OnActivityResult(ANOTHER_REQUEST_CODE)

voidonResult(Intent data){} @OnActivityResult(ANOTHER_REQUEST_CODE)


  • @OrmLiteDao – Field annotation for injecting OrmLite DAOs
  • @RoboGuice – Combine Android Annotations with RoboGuice dependency injection without subclassing
  • @Trace – Method annotation that logs entry and exit for the method
  • @Transactional – Method annotation that executes method in a SQLite transaction
rest api
  • Rest API lets you easily create REST clients using clean interfaces.
  • It is a wrapper around the Spring Android RestTemplate Module.
  • Android Annotations lets you reduce the amount of code to create an Android app
  • Takes care of the boilerplate code for you.
  • The work is done at compile time so does not slow app down (unlike RoboGuice)