Contents

Mobile App Development: Tutorial How To Use Android Support Annotations Library

1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 5.00 out of 5)
Loading...

 

The Android Support Annotations Library allows you to provide hints to code inspections tools like lint to help detect subtle code problems. They are added as metadata tags and you attach them to variables, parameters, and return values to inspect method return values, passed parameters, and local variables and fields. When used with code inspections tools, annotations can help you detect problems, such as null pointer exceptions and resource type conflicts.

Tung Dao Xuan, tungdx1210@gmail.com, is the author of this article and he contributes to RobustTechHouse Blog

 

 

Using The Library

Here we show you some examples for using Android Support Annotations Library.

 

2.1 Nullness Annotations

@NonNull

@Nullable

 

Add @Nullable and @NonNull annotations to check the nullness of a given variable, parameter, or return value. For example, if a local variable that contains a null value is passed as a parameter to a method with the @NonNull annotation attached to that parameter, building the code generates a warning indicating a non-null conflict.

Example for @Nullable :

Tutorial How To Use Android Support Annotations Library

 

Example for @NonNull :

Tutorial How To Use Android Support Annotations Library

 

2.2 Resource Type Annotations

@AnimatorRes

@AnimRes

@AnyRes

@ArrayRes

@AttrRes

@BoolRes

@ColorRes

@DimenRes

@DrawableRes

@FractionRes

@IdRes

@IntegerRes

@InterpolatorRes

@LayoutRes

@MenuRes

@PluralsRes

@RawRes

@StringRes

@StyleableRes

@StyleRes

@XmlRes

 

Add @StringRes annotations to check that a resource parameter contains a R.string reference. During code inspection, the annotation generates a warning if a R.string reference is not passed in the parameter.

For example for @StringRes:

Tutorial How To Use Android Support Annotations Library

For example for @DrawableRes:

Tutorial How To Use Android Support Annotations Library

 

For example for @MenuRes:

Tutorial How To Use Android Support Annotations Library

 

2.3 Threading Annotations

@UiThread

@MainThread

@WorkerThread

@BinderThread

 

If your method should only be called from a specific type of thread, you can annotate it with one of these 4 annotations.

Add @WorkerThread to specify the method should only be called from worker thread.

Example:

Tutorial How To Use Android Support Annotations Library

2.4 Value Constraints

@Size

@IntRange

@FloatRange

 

Add @IntRange or @FloatRange to specify your parameter must be in a certain integer, long or float, double range.

Tutorial How To Use Android Support Annotations Library

Add @Size to specify constraints on the size of the collection, length of arrays and the length of strings.

Tutorial How To Use Android Support Annotations Library

 

2.5 IntDef/StringDef: Typedef Annotations

@IntDef

@StringDef

 

Use the @IntDef and @StringDef annotations so you can create enumerated annotations of integer and string sets to validate other types of code references, such as passing references to a set of constants.

The following example illustrates the steps to create an enumerated annotation that ensures a value passed as a method parameter references one of the defined constants.

Tutorial How To Use Android Support Annotations Library

The line “@Retention(RetentionPolicy.SOURCE)” tells the compiler that usages of the new typedef annotation do not need to be recorded in .class files.

@StringDef is used for the same purpose but for strings.

 

2.6 RGB Color Integers

@ColorInt

 

You can specify @ColorRes when your API expects a color resource, but that won’t help when you have the opposite scenario: your API doesn’t expect a color resource id, but an actual RGB or ARGB color integer.

In that case, you can use the @ColorInt annotation, which states that an integer is expected to represent a color integer:

Example:

Tutorial How To Use Android Support Annotations Library

 

2.7 Overriding Methods

@CallSuper

 

If your API allows callers to override a method, but you need your own method to be invoked from any overriding methods as well, you can annotate the method with @CallSuper.

Tutorial How To Use Android Support Annotations Library

 

2.8 Return Values

@CheckResult

 

If your method is returning a value that callers are expected to do something with, annotate the method with @CheckResult.

Tutorial How To Use Android Support Annotations Library

 

 

 

References

Android Support Annotations Library references:

http://developer.android.com/reference/android/support/annotation/package-summary.html

http://tools.android.com/tech-docs/support-annotations

 

Conclusion

The Android Support Annotations Library is very easy to use and it helps you catch bugs very effectively. Hope you enjoy it. Thanks for reading!

 

 

If you like our articles, please follow and like our Facebook page where we regularly share interesting posts.  

RobustTechHouse is a leading tech company focusing on mobile app development, ECommerce, Mobile-Commerce and Financial Technology (FinTech) in Singapore. If you are interested to engage RobustTechHouse for your projects, you can contact us here.

 

 

Recommended Posts
Showing 7 comments
  • fallout 76 cheats

    google brought me here. Cheers!

  • Hanna

    When buying or selling real estate, you can often encounter fraud and disguised construction defects. The Building Inspection Sydney will give you a clear idea of ​​what problems the property may have and can help with your negotiations.

  • Lauriel Racher

    Introduce excitement to your gaming with the new and improved Dynamons World Mod APK. With its advanced graphics, innovative gameplay, and completely immersive environment, you are sure to find hours of entertainment and challenge.

    Take on the role of a captian and lead a team of Dynamons through the mysterious world filled with over one hundred species. You’ll need to carefully choose which ones you bring along, as each species has their own unique attacks and abilities. No two games will ever be exactly alike! Download link: https://techtodown.com/dynamons-world-mod-apk-1/

  • Bigg Boss 17 live

    Thanks to share such a great post. I am really aprreciate your work and realy enjoy and fun watch scuh type of drama I am really ipressed this amazing work.

  • Thanks for Watching Bigg Boss 17 Today Full Episode.

  • Bigg Boss 17 Today Full Episode

    Watch Online Bigg Boss 17 Today Full Episode Updated Video Show By Color TV.

  • 3456

    Thanks to share great article.plz share such type of article.
    2d lotto result summary

Contact Us

We look forward to your messages. Please drop us a note for any enquiries and we'll get back to you, asap.

Not readable? Change text. captcha txt
ecommerce7Tips