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.
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!
Similar Reads
How To Utilize ChatGPT To Improve Your Coding Skills? Chat-GPT is a large language model chatbot developed by Open-AI. It is trained on a massive dataset of text and code, and it can generate text, translate languages, and answer questions. Chat-GPT can be used to improve coding skills in several ways. It can provide quick answers to simple questions a
15 min read
How to Create Your Own Annotations in Java? Annotations are a form of metadata that provide information about the program but are not a part of the program itself. An Annotation does not affect the operation of the code they Annotate. Now let us go through different types of java annotations present that are listed as follows: Predefined ann
5 min read
Android Annotation Processing You've undoubtedly seen a lot of annotations as an Android developer: They're those strange code components that begin with @ and sometimes have parameters connected to them. Annotations link metadata to other code components, allowing you to include extra information in your code. Annotations may b
4 min read
How to Use ChatGPT to Complete Your Coding Assignments? In the fast-paced landscape of the digital era, characterized by the sweeping wave of automation and the transformative power of artificial intelligence, individuals from all walks of life, be they students or seasoned professionals, find themselves on a constant quest for ingenious methods to strea
8 min read
7 Tips to Improve Your Android Development Skills Android is an operating system which is built basically for Mobile phones. It is based on the Linux Kernel and other open-source software and is developed by Google. It is used for touchscreen mobile devices like smartphones and tablets. But nowadays these are utilized in Android Auto cars, TV, watc
5 min read
Java For Android - Building Your First Android App Android app development can play a major role in writing the app functionalities using Java programming. Every activity can be designed with Java programming. Android apps are developed using the Android Studio IDE, which provides the environment for Java development for Android programming.Prerequi
3 min read