The reference of the view id_name
is stored as type Int
, since compiling your program generates an entry in the R.java
file that refers to this resource.
For example if we create a view:
<TextView
android:id="@+id/id_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
inside the file R.java
that stores references of the resources of your application, an entry will be generated inside the class id
:
public static final class id {
...
public static final int id_name=0x7f0f0096;
...
Therefore you have to add R.id
to be able to directly access the element reference, otherwise you will not be able to access it; Class R
where the resources of your application are registered, Class id
, variable id_name
findViewById(R.id.id_name)
Inside the file R.java
you also have another type of classes that also define the type of elements they contain, such as:
public static final class drawable {
...
...
public static final class id {
...
...
public static final class dimen {
...
...
public static final class color {
...
...
public static final class anim {
...
...
public static final class array {
...
...
Why should I use: findViewById (R.id.id_name) and not only
findViewById (id_name)?
To access the elements within these classes it is important to add the class in which they are found, in this case id_name
is within class id
.