Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are "tag" and "id" on Layouts?

Tags:

I know how the switch statement works but I don't know what this means (R.id.webbutton). Can anyone please explain what it is and also what is TAG? Is there any guide for the beginners? I mean absolute beginners.

like image 290
vishalmullur Avatar asked Mar 18 '12 08:03

vishalmullur


People also ask

What is layout tag?

The <layout> tag must be the root tag when you are using DataBinding . Doing so you are telling the compiler that you are using DataBinding and your layout will have special tags like <variable> or <import> , so you have to embed your layout within that tag.

What is the difference between tag and ID in views?

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 are tags used for in Android?

Tag is an immutable object that represents the state of a NFC tag at the time of discovery. It can be used as a handle to TagTechnology classes to perform advanced operations, or directly queried for its ID via getId() and the set of technologies it contains via getTechList() .

What is the function of the layout tag?

A layout defines the structure for a user interface in your app, such as in an activity.


2 Answers

IDs and Tags

IDs

Views may have an integer id associated with them. These ids are typically assigned in the layout XML files, and are used to find specific views within the view tree. A common pattern is to:

Define a Button in the layout file and assign it a unique ID.

<Button    android:id="@+id/my_button"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/my_button_text"/> 

From the onCreate method of an Activity, find the Button

Button myButton = (Button) findViewById(R.id.my_button); 

View IDs need not be unique throughout the tree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching.

Tags

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.

Tags may be specified with character sequence values in layout XML as either a single tag using the android:tag attribute or multiple tags using the child element:

 <View ...        android:tag="@string/mytag_value" />  <View ...>      <tag android:id="@+id/mytag"           android:value="@string/mytag_value" />  </View> 

Tags may also be specified with arbitrary objects from code using setTag(Object) or setTag(int, Object).

like image 97
Kri Avatar answered Sep 27 '22 01:09

Kri


Id is id of your xml's components [may be views like textview,edittext... or viewgroup like linearlayout ,relativelayout... or anything else] in xml simply you can get reference to them in java code by saying

(R.id."id of your view in xml")

but firstly you should use setContentView(R.layout."name of xml file in layout/res in your project") this xml file which you want to use it's components .

TAG i use it when i want to show message in logcat [tool in eclipse you can watch your app messages when it is running] by saying String TAG= yourclassname.class.getsimpleName();

and use it in Log.d(TAG,"any string here"+some variable in my class i want to know it's value in a particular time when app running );

i hope that i made it clear to you .

like image 28
Weloo Avatar answered Sep 26 '22 01:09

Weloo