-9
<TextView
  android:id="@+id/button01"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:text="Sample"
  />

In this code, what does

android:id="@+id/button01

mean? And when do we use that id?

Tom
  • 16,842
  • 17
  • 45
  • 54

3 Answers3

3

As said in Documentation

ID

Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. When the application is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute. This is an XML attribute common to all View objects (defined by the View class) and you will use it very often. The syntax for an ID, inside an XML tag is:

android:id="@+id/my_button"

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file). There are a number of other ID resources that are offered by the Android framework.

Example in the same doc :

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

Now you can uniquely identify this button with

Button myButton = (Button) findViewById(R.id.my_button);
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
1
<TextView
    android:id="@+id/button01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Sample"
/>

This means that the TextView has a unique ID: button01 which can be used to access it in the application. To access it, we use:

TextView text = (TextView) findViewById(R.id.button01);

Operations can then be performed on this View, e.g.

text.setText("hello");
Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
1

A unique resource ID defined in XML. Using the name you provide in the element, the Android developer tools create a unique integer in your project's R.java class, which you can use as an identifier for an application resources (for example, a View in your UI layout) or a unique integer for use in your application code (for example, as an ID for a dialog or a result code).

Ref: http://developer.android.com/guide/topics/resources/more-resources.html#Id

scott
  • 3,112
  • 19
  • 52
  • 90