What are "tag" and "id" on Layouts?
Asked Answered
C

3

24

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.

Craiova answered 18/3, 2012 at 8:45 Comment(0)
L
9

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 .

Leavy answered 18/3, 2012 at 9:3 Comment(2)
why do you have to use the word TAG? why cant you use other names?Vonnievonny
@Vonnievonny it's just a conventionCahan
Y
82

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).

Yorgen answered 18/3, 2012 at 8:57 Comment(3)
Thanks for the that link. It explained most of my doubts.Craiova
If I put a tag in my layout as in those two examples in the Tags section above, how do I get the information out programmatically?Destiny
not sure what "tag" OP was asking about, but this answer helped me.Bergerac
L
9

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 .

Leavy answered 18/3, 2012 at 9:3 Comment(2)
why do you have to use the word TAG? why cant you use other names?Vonnievonny
@Vonnievonny it's just a conventionCahan
H
2

Start with the tutorials. (If you are so absolutely a beginner that you don't have a development environment set up yet, then start with Installing the SDK.)

When you use the console log facility in Android, the first argument to the logging methods is a tag, which can be used to filter logcat output. A typical programming style is:

public class Something { 
    private static final String TAG = "Something";

    public void aMethod() {
        Log.i(TAG, "Entered aMethod");
    }
    . . .
}

That's what TAG is.

Resource IDs are explained in the tutorial. When you define a resource in XML, Android generates a class called R with nested classes for different kinds of resources (R.id, R.string, R.layout, etc.). Each of those nested classes has a constant for each resource of that type. R.id.webbutton might be generated from a layout file that has a button with attribute android:id="@+id/webbutton". This is all explained in the tutorials.

Honeysucker answered 18/3, 2012 at 8:53 Comment(6)
I am reading the tutorials but I still find it a lot difficult to understand all of it. I know those tutorials are probably the best available but I think I need a few more weeks to go thorugh all of them. Thanks a lot.Craiova
a tag can be applied to an object with setTag and getTagArbitrament
@Arbitrament - Yes, but that's almost certainly different from the TAG (all upper case) that confused OP when it was encountered in code. A huge amount of Android sample code uses a TAG string constant when it shows anything being logging.Honeysucker
@TedHopp I think OP is asking about view tags not logging tagsPentaprism
@vlad - If that were the case, TAG wouldn't be all upper-case in the question. The all-upper-case TAG is usually seen in Android sample code, as well as in template code in Android Studio, as the logging tag (as I show in my answer). Also, since OP accepted Weloo's answer, which makes the same assumption I did, it's pretty clear that OP did not mean view tags.Honeysucker
@TedHopp you make a good point, I think you're right. I didn't even notice the capitalization at first and figured the question was in the context of views so it must be about view tags. My mistake.Pentaprism

© 2022 - 2024 — McMap. All rights reserved.