How to Improve Your Android Coding Through Annotation?
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
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.
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.
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.
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:
If you write anything like this:
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.
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.
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!
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.