Open In App

How to Improve Your Android Coding Through Annotation?

Last Updated : 05 Aug, 2021
Like Article

Annotations are a type of metadata. Metadata, on the other hand, is a collection of data that provides information about other data. There are numerous ways to use annotations in Android. However, in this section, we will discuss how annotations may be utilized to better our Android coding. Officially, Android already has support annotations, which you may incorporate via the dependence as seen below

compile ‘

Here we are discussing some great annotations which will surely help you.

Annotations of Nullness

The annotations @Nullable and @NonNull are used to verify the nullness of a variable, argument, or even the return value.

  • @Nullable: This class denotes a variable, argument, or return value that can be null.
  • @NonNullable: A variable, parameter, or return value that cannot be null.


public View aFunction(@Nullable String gfg1, @NonNull String gfg2) {
 // gfg1 can be null
 // gfg2 cannot be null
 // a non null view is required as a return

As a result, when you call this function as shown below. During code examination, Android Studio will tell you that gfg2 should not be a null value.


View view = aFunction("Spandan", null);

Annotations to Resources

Because Android references to resources, such as drawable and text resources, are provided as integers, the resource types must be validated. Code that expects a parameter to correspond to a certain type of resource, such as Drawables, may be passed the anticipated reference type of int.


public void setText(@StringRes int gfgResID) {
  // gfgResID is a string.

And then call this method like below:



If an R.string is not given in the argument, the annotation raises a warning during code inspection.

Annotations to Threads

Thread annotations determine whether a method is called from the thread type from which it is meant to be invoked.

Annotations that are supported include:

  • @BinderThread
  • @WorkerThread
  • @MainThread
  • @AnyThread
  • @UiThread

If you write anything like this:


public void aFunction(){
  // this is the method being called

GeekTip #1: If you call this method from a thread other than the worker thread, the warning will be shown.

Annotations with a Value Constraint

When we need to limit the parameters, we may use the @IntRange, @FloatRange, and @Size annotations to check the values of provided parameters. These are helpful when the method’s caller is likely to supply the incorrect value (out of the specified range). In the following example, the @IntRange annotation guarantees that the integer value supplied must be in the range of 10 to 200.


public void setBeta(@IntRange(from=10,to=200) int beta) {}

Annotations with Permission

To validate the rights of a method’s caller, use the @RequiresPermission annotation. The setWallpaper() function is annotated in the following example to guarantee that the method’s caller has permission.

Permission to use SET WALLPAPERS

If you call this method without having the appropriate permission in the manifest, a warning will appear.


public abstract void functionForWallPaper(Bitmap gfgLogo) throws IOException;


With this we end our discussion, you may read other great GfG articles posted on the portal, hope this article helps you to find your way to Android Annotations!

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads