0

In the android development:

Normally a view is assigned an ID to distinguish itself from other views. That ID is given a string value in the XML. The method findViewByID(int id) takes an integer parameter to get a view.

We assign a tag to the view in the XML using the attribute android:id.

What value does the method findViewById() take and how the numeric ID's are generated which we use it for tracing any view from the View Hierarchy ?

Thanks

Alan
  • 1,479
  • 3
  • 20
  • 36
Pinkesh
  • 59
  • 1
  • 8
  • refere http://www.tutorialspoint.com/android/android_hello_world_example.htm Check R file in it it is resource id automatically generate in R file – Vickyexpert Jun 14 '16 at 12:25

3 Answers3

3

When you want to declare an id in XML you do it as android:id="@+id/myId" R is Java class. When you include the above line for an XML view a public static final int myId field gets included into the R class. You can reference this from your own classes.

findViewById(int) accepts an integer as a parameter. The R class contains Integers and not the strings you entered as the XML id.

Here is a sample from an R class.

public final class R {
   public static final class id {
      public static final int ReflectionsLevelText=0x7f0d00af;
      public static final int about=0x7f0d01b3;
      public static final int action0=0x7f0d014d;
      public static final int action_bar=0x7f0d005f;
      public static final int action_bar_activity_content=0x7f0d0000;
      public static final int action_bar_container=0x7f0d005e; 
   }
}

So if you want to access the view with the id action_bar you have to call findViewById(R.id.action_bar)

In the same way R class also includes drawables, dimensions and basically all the resources. They are exactly inner static classes inside the R class.

For an example when you add a drawable ic_my_pic.png to res/drawable a field gets generated in the R class. It would look like,

public final class R{
    public static final class drawable{
         public static final int ic_my_pic=0x7f020000;
    }
}

Now you can access this image from your classes by,

imageView.setImageResource(R.drawable.ic_my_pic);

You can find more info here and here.

Community
  • 1
  • 1
Malith Lakshan
  • 762
  • 6
  • 12
  • From the above example, the class_name 'id' is the name we provide it to the view ? – Pinkesh Jun 14 '16 at 12:56
  • In oder to declare an id in XML you have to use @+id/ format. It means that the value you enter as the id has to be included in t id inner class of R class. It does not mean that you can enter any thing as the andorid:id . Updated the answer.@pinku – Malith Lakshan Jun 14 '16 at 13:06
0

When gradle builds your app, it generates a "

<EditText 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/myEditText" />

You would then reference it in your code using the generated R class:

EditText myEditText = (EditText) findViewById(R.id.myEditText);
Josh Kitchens
  • 1,080
  • 11
  • 18
0

All of the views in a window are arranged in a single tree. You can add views either from code or by specifying a tree of views in one or more XML layout files.

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.

Reference : https://developer.android.com/reference/android/view/View.html

KishuDroid
  • 5,411
  • 4
  • 30
  • 47