Android R.java use - android

When we add some entries in the strings.xml file or layout.xml file, then the R.java file gets modified automatically. Again, if we want to refer something from the layout file such as reading the EditText value entered by the user, then again we refer the R.java file at our java code to read the values.
What is this R.java file all about? The values of each entry of this R.java file seems to be in HEXADECIMAL format but what is its use?
I have read the doc but i get fairly confused for this R.java :(
Please someone step forward and explain what is this R.java file all about :(
Regards,

http://developer.android.com/guide/topics/ui/declaring-layout.html says:
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).
The R.java file is generated by the Android Resource Manager (aapt.exe) and contains references to all resources of your app. Each reference is a unique id (public static final int). These constants are written to the R.java file in hexadecimal format. The logic of assigning specific integer to each resource is private to the Android Resource Manager. You can look at the source code of aapt.exe on the internet, e.g. at http://gitorious.org/rowboat/frameworks-base/trees/d58fb97ddf052b3ceac921ac7e936af990392b2c/tools/aapt

It turns resource objects into Java recognizable names for you to refer to in your code.

The R class is basically the way Android provide resource access from within your code. As you wrote when you alters strings.xml etc on save of that resource file Android SDK will recompile the R class to make your changes accessible for within your code. What the values in R class are is more or less not important since its used by Android internally to map, for example a string to an ID.
To reference a string from within your code you use R like this:
R.string.MyString
If you string in string.xml is called MyString. Same for layouts etc.
I guess this is what you read, but otherwise it's pretty good explained here.

Related

why we put # before using resources in android xml file?

Why put # in xml file of android to access some resources?
<android:background="#drawable/coloreffect">
why put before # in xml file of android to access some resources?
That is to distinguish a reference to a resource from other things.
For example, not only can you have android:background="#drawable/coloreffect", but you can have android:background="#ffff0000" to refer to a specific color. Not only can a TextView have android:text="#string/foo", but it can have android:text="Foo", for a hard-coded string. And so on.
It is used to differentiate normal strings and string referred to resources.
For example when you write
android:background="resource name without #"
then it act as static value it does not bind from your resources.
The special character # is used to indicate that the following string will reference a resource file.
If you don't put it then drawable/coloreffect is interpreted as a simple string.
Android follows different syntax for referring to resources, styles etc.
Resources can be referenced from resources using a special XML syntax, such as #drawable/myimage
To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (#), use a question-mark (?), and the resource type portion is optional.
For more details https://developer.android.com/guide/topics/resources/accessing-resources.html
Hope this helps.

Android: capital letter in filename of xml file, and the rule of naming a xml file

It's ok to use capital letter in java file name in android project, but captial letter is not allowed in xml file name. What is the profit of this design?
I used to name xml file like this:activity_xxx. Is there any suggestion or rule in naming?
It seems to be no official answer to this question.
Resources are defined automatically in R.java. As an example, if you have a layout named activity_test then it will generate public static final int activity_test=0x7f040034; in R.java.
That generation doesn't follow android naming conventions for the constants. (See)
In my opinion, the Android dev team choosed to avoid this convention in this generated file in order to speed up the typing of the resource names (it is easier to type a lowercase letter than a capital letter)
If a project is created on Android Studio, the basic resources are prefixed by "activity_" for activities, "fragment_" for fragments, "menu_" for menues so it seems to be right way to write an xml filename.

What is # in "#+id/idname" format mean in android?

official link here
They say:
The at sign (#) is required when you're referring to any resource object from XML. It is followed by the resource type (id in this case), a slash, then the resource name (edit_message).
I'm not quite understand what # mean, who can give a more specific explanation?
If the ID is not already created, then + will create it. You will need it only the first time you refer to that id.
From docs :
The plus sign (+) before the resource type is needed only when you're
defining a resource ID for the first time. When you compile the app,
the SDK tools use the ID name to create a new resource ID in your
project's gen/R.java file that refers to the EditText element. With
the resource ID declared once this way, other references to the ID do
not need the plus sign.
Like the docs say, the # symbol means you are referring to an xml resource.
For example : #string/edit_message means you are trying to refer to a string with the id edit_message defined in an xml file.
# sign means you are referring to a resource under res folder.
#drawable
refer to a .png or button state images.
#anim
refer to animation
#id/anyID
refer to any pre Created id
#+id/createNewID
+ create a new id and name it createNewID and
+sign only works with id. you cannot use it with anim,drawable.
Note
You can not create any Resource programatically other than id.
# is used to give identity or to refer to the existing resource in XML File.
'#' specifies what kind of resource you are about to access.
if you use #id, it will refer to any resources in layout of project.
if you use #string, it will locate to a string that you may usually specify in Strings.XML
there are so many other resources like :
#drawable, #color, #style, #array, #dimen, #layout & #xml..
you can use according to your requirement.
In each android project there is a gen folder inside src where R.java is created automatically and all the IDs of resources used/created in XMLs belong here.
As the docs say:
The at sign (#) is required when you're referring to any resource object from XML.
This means that you are going to refer an ID from R.java if you add + then the corresponding id will be created if it does not exists in R.java already. As the docs say:
The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's gen/R.java file that refers to the EditText element. With the resource ID declared once this way, other references to the ID do not need the plus sign.
So + sign is required only when you want to create a new id for the view. If it is already created in the same file then it will simply reuse that id instead of creating a new id with the same name.
NOTE: you can use + with IDs only.
# has so many uses. The one which are used mostly are:
#id/ to reference from R.id
#string/ references R.string similarly...
#drawable/ -> R.drawable
#array/ -> R.array
#color/ -> R.color
Finally you can use #android to refer resources defined in the android SDK.

What happens if two resources differ only in their extension in andorid?

I have a small confusion, when in drawable folder in android i try to copy same file it says that "Resource with this name already exists" which is absolutely correct. Again if try to copy an image with different .extension but with same file name it takes it. But in R.java file only 1 resource id is generated. What does this mean? Also ID will point to which resource?
It simply means that at the time of compilation it is only able to generate id from one of the available resource. Why can't it generate both id's if it has allowed to keep both resources in the same folder? Because for each resource, there is a static integer named after the name of file, excluding extension so naturally, there can't be more than one of these static integers in one file.
ID points to which resource? I think you can't be sure. I just checked by recreating the scenario (out of curiosity) and I found that it pointed to the resource I added in the end!
Still, it is irrelevant as to which resource it points to. Because it won't allow to run the app.
P.S: If you really want to use resources with same name (diff extension) then you can do so by using assets

How does the mapping between android resources and resources ID work?

It is magical for Android to locate the proper resource just through the R.id.XXX.
AFAIK, the resources are compiled to binary format, so how does this mapping logic work under the hood?
Maybe it works like this:
For e.g., in the layout1.xml, we got:
<Button android:id="#+id/button1" >
and AAPT will generate this in the R.java:
public static final int button1=0x7f05000b;
When the *.apk is genrated, the #+id/button1 with be substituded with "0x7f05000b".
Thus, when we call:
findViewById(R.id.button1);
we are essentially still do the search based on the ID, though the ID is a number like 0x7f05000b.
Thanks!
ADD
What I really want to know, is how the resource id integer is parsed into the resource content? In other words, how does the Android runtime locate the resource content with resource id as the sole clue?
For example, how is a drawable picture found with a resource id? Or how is a string value is found with a resource id?
At build time, the aapt tool collects all of the resources you have defined (though separate files or explicit definitions in files) and assigns resource IDs to them.
A resource ID is a 32 bit number of the form: PPTTNNNN. PP is the package the resource is for; TT is the type of the resource; NNNN is the name of the resource in that type. For applications resources, PP is always 0x7f.
The TT and NNNN values are assigned by aapt arbitrarily -- basically for each new type the next available number is assigned and used (starting with 1); likewise for each new name in a type, the next available number is assigned and used (starting with 1).
So if we have these resource files handled by aapt in this order:
layout/main.xml
drawable/icon.xml
layout/listitem.xml
The first type we see is "layout" so that is given TT == 1. The first name under that type is "main" so that is given NNNN == 1. The final resource ID is 0x7f010001.
Next we see "drawable" so that is given TT == 2. The first name for that type is "icon" so that gets NNNN == 1. The final resource ID is 0x7f020001.
Last we see another "layout" which has TT == 1 as before. This has a new name "listitem" so that gets the next value NNNN == 2. The final resource ID is 0x7f010002.
Note that aapt by default makes no attempt to keep these identifiers the same between builds. Each time the resources change, they can all get new identifiers. Each time they are built, a new R.java is created with the current identifiers so your code gets the correct values. Because of this, you must never persist resource identifiers anywhere where they can be used across different builds of your app.
Once the resources are compiled and identifiers assigned, aapt generates the R.java file for your source code and a binary file called "resources.arsc" that contains all of the resource names, identifiers, and values (for resources that come from separate file, their value is the path to that file in the .apk), in a format that can easily mmapped and parsed on the device at runtime.
You can get a summary of the resources.arsc file in an apk with the command "aapt dump resources <path-to-apk>".
The format of the binary resource table is documented in the header file for the resource data structures here:
https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/include/androidfw/ResourceTypes.h
The full implementation for reading the resource table on the device is here:
https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/ResourceTypes.cpp
If you are interested in the internal implementation (device side) have a look at loadDrawable() in Resources.java. Refer to hackbod's excellent answer for information about extracting data from the resource table
To know how layouts are translated into View's from resource ID's check out LayoutInfater.java
From what I understand, aapt will auto-generate unique IDs for each of your resources and store them in a look-up table. This look-up table is persisted as the "resources.arsc" file located in "bin/resources.ap_" (this is just a ZIP file, so feel free to open using your favorite ZIP viewer). The look-up table is also persisted as R.java, which as you know allows you to reference your resources in Java.
If you want more information on the ARSC file, I would suggest Googling it, or reviewing the code of http://code.google.com/p/android-apktool/.
-Dan
One final note: for the longest time, I didn't use relative layouts because many items need to reference items further down in the xml file, and I didn't know how to reference an #id/foo that hadn't been defined yet.
<!-- doesn't work -->
<TextView android:layout_above="#id/foo">above</textview>
<TextView android:id="#+id/foo">below</textview>
Then one day I realized (duh) that you can define an id in the reference; it doesn't have to be in the element that bears the id:
<!-- works -->
<TextView android:layout_above="#+id/foo">above</textview>
<TextView android:id="#id/foo">below</textview>
The magic is in the Eclipse plug-in and the R.java file it autogenerates in an app's "gen" folder. If you peek into this file, you'll see static mappings for every XXX in R.xx.XXX where xx can be anim, array, color, and every other resource type.

Categories

Resources