Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of android:tag parameter in xml?

Tags:

android

xml

tags

I saw a xml layout which has a textView as below

       <TextView
            android:id="@+id/tvHeaderTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Change password"
            android:tag="@string/regular"
            android:textAllCaps="true"
            android:textColor="@color/header_color"
            android:textSize="@dimen/font30" />

I want to know what is android:tag property is used for. A detailed answer for be greatly appreciated.

Note - I read that it is used for data binding, but could not follow the context.

Update 1 - (Thanks Blackbelt for the answer) Usages 1. Could be used for compile time binding of xml elements with the activity. Although android recommends the use of id. 2. Could be used for identifying elements for list adapter. For eg, a special case of a list adapter with multiple sort support. Then we could use the tag element to identify the desired list item.

I would be glad to know if it could also be used for assigning any other property?

like image 319
B.B. Avatar asked Feb 11 '16 15:02

B.B.


People also ask

What is Android tag in XML?

XML tags define the data and used to store and organize data. It's easily scalable and simple to develop. In Android, the XML is used to implement UI-related data, and it's a lightweight markup language that doesn't make layout heavy.

What is the use of Android tag?

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

What is tagging in XML?

It allows to create new tags (user defined tags). The first element of XML document is called root element. The simple XML document contain opening tag and closing tag. The XML tags are case sensitive i.e. <root> and <Root> both tags are different. The XML tags are used to define the scope of elements in XML document.

What is the difference between app and Android in XML?

android is usually used for attribute coming from Android SDK itself. app is often used if you are using the support library. You may also see other namespaces if you are using custom views (of your own or form a library).


1 Answers

I want to know what is android:tag property is used for

it is a different way to add associate additional information with the view object itself. In you case your tag is a string, but it can store complex objects too. For instance the tag could be a model class set and retrieve at runtime using the setTag()/getTag() pair. The way this information is used is up to the users. Android allows to findViewWithTag too. In your case, for instance you could look for the same object using findViewById(R.id. tvHeaderTitle); or using findViewWithTag(getString(R.string. regular)); . An example of usage is with ListView when you have a button part of you item and, upon click you want to know the which item in your dataset is associated with that row. Using the pair setTag/getTag this thing is easily achievable

like image 166
Blackbelt Avatar answered Nov 05 '22 22:11

Blackbelt