Difference between id_name and R.id.id_name

1

I'm starting with android and I'd like to understand this difference.

I notice that if I have a created "view" of name id_name, if I inspect id_name I see that this is a value of type int.

However, if I want to use findViewById() , the format says I should use: findViewById(R.id.id_name) . I understand that R is the class that stores the Resources, but by debug I noticed that of course "R.id.id_name" is also an int value (type required by findViewById() ) and this value is exactly equal to the value of id_name .

Then:

  

Why should I use: findViewById(R.id.id_name) and not only    findViewById(id_name) ?

    
asked by Tony Rodriguez 08.10.2017 в 20:36
source

3 answers

0

Well, because that id is stored in the variable R, although the two are the same, the id_name is simply telling you that it is of the integer type but it is stored in the variable R.

You have to use R.id_name to access the same as with other R.string elements. etc. Since all these key values are stored and classified in R.

    
answered by 08.10.2017 в 22:33
0

What you mention that you can use the id_nombre directly is because you are doing a import static of the property:

import static nombreProjecto.R.id.id_nombre;

If you go to the declarations of the packages you will see the definition of the import. Actually it is the same as using R.id.id_nombre , only that with the static import you should not use the complete reference of the field id_nombre . If you want to do it with another property you will have to add the import in the declaration of the packages.

There are also 2 ways to achieve this in addition to import static :

1.

Assigning the value of R.id.id_name to a variable and it will be valid for the analyzer:

int id_name = R.id.id_name;
TextView nombre = (TextView)findViewById(id_name);

2.

Disable the studio android survey to allow you to enter any id by clicking ctrl+enter on the error - > Inspection 'Constant and Resource Type Mismatches' options - > Disable inspection:

Although it is not recommended because you would have to learn the id of the element that is an integer.

    
answered by 09.10.2017 в 00:28
0

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 .

    
answered by 09.10.2017 в 16:48