Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Improve Your Android Coding Through Annotation?

  • Last Updated : 05 Aug, 2021

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 ‘com.android.support:support-annotations:latestVersionHere

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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.

Java






@NonNull
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.

Java




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.

Java




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

And then call this method like below:

Java




someTextView.setText(30);

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:

Java




@SomeWorkerThread
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.

Java




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.

Java




@RequiresPermission(Manifest.permission.SET_WALLPAPER)
public abstract void functionForWallPaper(Bitmap gfgLogo) throws IOException;

Conclusion

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!




My Personal Notes arrow_drop_up
Recommended Articles
Page :