Introducing a new <tag /> XML tag to attach info to Android Views
It is no secret, the Android framework has a very open, large and verbose API. It basically lets developers create everything they want as long as they have a clear idea of the final product. However, there are some cases where framework capabilities are not obvious because they are not well documented or just hidden in the verbose API. This is especially true when dealing with the Android View ecosystem. In this post I would like to discuss about how to attach information to a View.
The framework comes with several ways allowing developers to attach information to a View. Some of them have always been available while some other are relatively new. In order to better list these techniques, I have split the post in several parts. Each part relates to an API level.Android 1.0
Being a object oriented programming language, Java has always offered an easy way to add information to an object: inheritance. The language lets developers prevent this by either making the constructor private or the class final. But because Views have been designed for inheritance, it is quite simple to extend a given class and add information. For instance, you can add information to a TextView creating a TextViewWithData which extends TextView.
Inheritance works great but has one main problem : it is not global. If you want to be able to add info to all View classes, you have to extend all of them creating a "shallow" copy of the framework classes.
Actually, Android has always allowed developers to attach/retrieve information at the View level. This is done thanks to the setTag(Object) and getTag() methods. Android also provides a android:tag XML attribute you can use directly attach a String tag to a given View. In general, you should use findViewById(int) in order to retrieve a given View in a View hierarchy. However, there are some cases where getting a View with a particular tag can be handy. This can be done using findViewWithTag(Object).Android 1.6
API 4 introduced a new way to attach tag objects to Views. Indeed, both setTag(int, Object) and a _getTag(int) were introduced. The main advantage of these methods was it was possible to add several tags to a View. There are some requirements though: keys had to be a resource identifier (i.e.@+id/something). In practice, these methods were introduced to store references to child Views.
Although these methods methods looked nice it was strongly discourage to use them because the implementation was mostly leading to memory leaks. Indeed, tags were actually held in a static global pool. You can have a look at  and , and more specifically +Adam Powell
comment, to better understand the issue.Android 4.0
The internal implementation of setTag(int, Object)
finally changed in Android 4.0. A switch to a non static SparseArray local to the View itself was made. In other words, starting API 14, it is now safe to use setTag(int, Object)
to store references to child Views in the hierarchy.Android 5.0
Recently, API 21 introduced a brand new way to attach information to Views straight from the XML definition of a layout. A new XML tag <tag /> were in introduced in LayoutInflater allowing us from attaching text information to the parent View. If you like deep diving into the Android source code, you can have a look at +Alan Viverette
's commit. Here is an example of how to use it:values/ids.xml
<?xml version="1.0" encoding="utf-8"?>
<item format="reference" name="btn_state" type="id" />
The new <tag /> tag brings a new way to statically attach information to a View. It's now up to you to determine some great use cases to this new LayoutInflater-interpreted XML tag.