Aaron Liew's Blog

Blog about Android Development

Android Annotation

Android Annotation(AA) is framework that facilitate the writing and the maintenance of Android Applications, and greatly simplifies the code. It generates subclasses at compile time, for example, the subclass of the MainActivity class would be MainActivity_. To understand how the AA works, you can have a look at the subclass of the Activity.

How to enhance Android Code with AA

I would like to discuss the common annotations that would be used in Android Development.

Add @EActivity with layout id at the top of the class to replace setContentView of the Activity Class

1
2
@EActivity(R.layout.sliding_menu)
public class BaseActivity extends ActionBarActivity

Add @ViewById to replace findViewById

1
2
@ViewById(R.id.drawer_layout)
DrawerLayout mDrawerLayout;

Add @Bean to inject the activity context into class

1
2
@Bean
GlobalApplication globalApplication;

Add @AfterView before method to do something after view injection, for example, setText, setBackground, etc..

1
2
@AfterViews
public void initLayout()

Add @Click method with view’s id to handle OnClickListener.

1
2
@Click(R.id.about_us_linearLayout)
public void ClickAboutUs()

That’s it. There is no need to do “findViewById” to get the view then followed by setOnClickListener.

Want to run a thread? Add @Background above the method

1
2
@Background
public void DownloadAndSetImage()

Initiate the Layoutinflator using @SystemService

1
2
@SystemService
LayoutInflater inflator;

Add EBean at the top of the CLass in order to use available annotations

1
2
@EBean
public class GlobalApplication

Add @RootContext to inject the context into the class

1
2
@RootContext
static Context context;