Issue
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
Solution
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.
Answered By - Malith Lakshan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.