Whence construct R.id? - android

In some code we're trying out, coming from various tutorials, we end up missing R.id, which should be generated in R.java, obviously. We can add it in (by analogy with other "first" Android projects we've done), but as this file is auto-generated, anything we do like that just gets overwritten ultimately.
public static final class id
{
public static final int Button01=0x7f060004;
.
.
.
}
Was there a construct to put into strings.xml, main.xml, etc. that causes this to be generated?
(Yeah, we're total noobs. Sorry.)
Thanks for any help you can give,
Russ Bateman

Say I have an XML file with the following content:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView android:id="#+id/ListView01"
/>
</RelativeLayout>
I save it in res/layout/ .
Then R.id.ListView01 gets automatically created.
You might want to look at the Notebook Sample application and how it's organised. At least that's how I got familiar with androids organisation.

R.java is auto generated by Eclipse. If it is not built, this means you have probably an error somewhere in your xml files, or a resource with a name that is not allowed.
Sometimes it is just Eclipse doing strange things, in this case, you can try :
Project > Clean > all project
Then let Eclipse work.
Sometimes it solves the issue. If not, it's highly probable that you have an error somewhere in your resources.
To create this file, Eclipse gathers the ids you declared in your xml files with #+id,but also the layout names, images names, string names, ...

It appears that project/res/layout/main.xml contains the constructs that lead to the generation of id in R.java. (I'm not limiting the source for these only to that XML file.)
Look specifically for android:id in various widgets (I think they're called) such as TableLayout, TextView, EditText, etc. There will be corresponding
public static final class id
{
public static final int x=0x7f05006;
public static final int y=0x7f05000;
public static final int z=0x7f0500c;
.
.
.
}
where x, y, z correspond to the (TableLayout, TextView, EditText, etc.) identifier coded immediately after the "#+id/" construct in the XML file (and the initialized value in R.java is magically generated--you don't have to worry about that).
Thanks to all who contributed.

Related

wamy project's R.java file is causing and error and when ever i fix it eclipse removes the changes i made

this is causing the error
public static final class id {
public static final int timeToSend=0x7f080021;
public static final int timeToSend,=0x7f080023;
}
whenever I remove the second and save the file eclipse changes it back. I tried changing it while eclipse is not running and yet when I opened eclipse it changed the file back. I don't have any duplicates in layout to cause the problem. I tried cleaning the project and it didn't work.
turns out I wrote android:layout_alignBottom="#+id/timeToSend," instead of android:layout_alignBottom="#+id/timeToSend" that's why it nade a duplicate with the comma. sorry for the trouble
Maybe it's because you have an XML element with the android:id="#+id/timeToSend," ?
Android ID's cannot have any special characters in them. So android:id="#+id/timeToSend, would be wrong due to the fact that it has a comma. All you have to do is find that ID and rename it.
It's not recommended to edit the R.java file because it generates itself based on all the ID's you have created. The only way to edit it is to get rid of the ID's that you have declared in XML, otherwise, every time you clean the project, they regenerate.
Something that I always try when R.java is giving me problems is rebuilding the project.
You should be able to find the option under Project and then Clean.
That's only if rebuilding it hasn't worked.
Good luck and I hope this helps!

How to store resource file with same name but different extensions in android

I am working on one static custom Android Media Player Application where i have many media files stored in raw folder.
Where as some files have same name but different extensions, here android is giving me
res\raw\then.mp4:0: error: Resource entry then is already defined.
res\raw\then.mp3:0: Originally defined here..
Can anybody please suggest me something on this?
Also there are few media files with java keywords like if, else, return, switch, case,class, else, final, long, new, this,true... where android is giving me error of invalid symbol.
Please suggest me solution for that also.
Thanks in advance...
As #blackbelt said, it's not possible, I'll just add that you can instead put your files in the assets directory instead of res.
You will not be able to use them like R.id.file, but you will get more flexibility.
simply you can't. Raw is build at compile-time inside R.java, and the name key must follow the java convention for naming. Since
if, else, return, switch, case,class, else, final, long, new,
this,true
are reserved keywords you can not use them.
Edit: R.java would look lie:
public static final class raw {
public static final int if=0x70000;

How to create a string resource programatically

I am using WebTrends analytics in my app.
(Ref-http://help.webtrends.com/en/android/)
The WebTrends APIs are written in such a way that the initialization happens ONLY from resources file. They provide webtrends.xml with the sdk and all the parameters are set from the values from this xml file.
Now the problem is I have to set a couple of values dynamically based on our server feed.
Here I have only two choices:
(1) Set WebTrends initialization values in the code which looks impossible from WebTrends SDK. Neither the member variables are exposed outside the library nor there are any setters/methods to set the initialization params.
(2) Create resources from webtrends.xml dynamically or at least set the values for resources dynamically which also seems impossible.
Can anyone please suggest the way out of this deadlock?
You cannot do that. When you add a string resource an automatic entry is made for the resource in R.java file at compile time.
Example:
public static final class string
{
public static final int app_name=0x7f040000;
}
where app_name is the name of the string resource. So, it is not possible.

what R.java file actually does and how

I have been working on a simple android tutorial and while browsing through the project folders I found this R.java file in gen folder...
When I opened it seemed to me as a mess...
first R itself is a class.
it had multiple Inner classes defined within eg drawable,id,layout,etc.
and that inner classes had lots of variables declared as below which were assigned with hex values
public static final int addr=0x7f080003;
...
...
and much more
R is auto generated and acts as some pointer for other files
Questions for R.java
what it is basically for
how it works
why
values are in hex
what role did it performs while the actual application is running
"Acts as some pointer to other files" is actually absolutely correct, now the question is which files it points to how it is done.
What does it contain?
R file contains IDs for all the resources in the res folder of your project and also some additional IDs that you define on your own (in the layouts, for example). The IDs are needed for the Android resource management system to retrieve the files from the APK. Each ID is basically a number which corresponds to some resource in the resource management system.
The file itself is needed so you can access or reference the resource from code by giving the ID of the resource to the resource manager. Say, if you want to set the view in the activity, you call
setContentView(R.layout.main);
main in the R file contains the number which is understood by the Android resource management system as the layout file which is called main.
Why is it better than just plain file names?
It's harder to make a mistake with the generated fields. If you write the field name incorrectly, your program won't compile and you will know that there's an error immediately. If you write an incorrect string, however, the application won't fail until it is launched.
If you want to read more on this topic, you should check the Android documentation, especially the Accessing Resources part.
This holds your resource ids. So when you do something like
TextView tv = (TextView) findViewById(R.id.mytextview);
it looks up your id here for that View, layout, etc... This way the app has an easy way to look up your ids while you can use easy to remember names. Anytime you create a resource it automatically creates an id for it and stores it here. That's why you never want to try and edit this file yourself.
One way to think about how valuable R.java is, imagine a world without it. Its amazing how android brings the xml and java world together to help avoid coding the UI manually completely. With legacy java building UI using the java language was a pain. Invaluable.
With Android you can not only build your UI using only xml, but also see it while you build it. Invaluable.
Every element in the xml can be referenced in the java code WITHOUT writing a single line of code to parse the xml :). Just R.id.nameOfElement. Invaluable.
Rapid development is beautifully done in android. Imagine if iPhone would have 5000 screens to fit that one piece of code, they would crumble on their XCode. Google has done a wonderful job with just R.java. Invaluable.

Android layout view id name issue

I recently faced an issue when i named a view as android:id="#+id/TextView1" and the code
TextView tv1 = (TextView) findViewById(R.id.TextView1);
was returning wrong view even though there were no warnings given for the naming convention.
When i changed the name to text_view_1 it worked.
I know the usual naming convention is all small case like android:id="#+id/text_view_1" or camel case like android:id="#+id/textView1"
Is there any documented resource which explains the issue or any answer for it?
Also i want to know what m stands for in the variable name which i often see in open source code. e.g.
TextView mTextView;
Its a noob question but i couldn't find a answer anywhere.
Try to clean and build you project.
Android coding syle guide states:
Non-public, non-static field names start with m.
Static field names start with s.
Other fields start with a lower case letter.
Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
It would be private ImageButton mButtonStart; or private static sSingletonThing;
That convention is by the way only required to be used if you want to contribute to the Android sourcecode, you can write your own apps in any style.
Thats a known issue in Eclipse. It was returning the wrong view because there must have been some referencing issues. Generally cleaning the project or restarting eclipse helps.
And regarding the prefix m, 'm' is a naming convention used to denote private member variables and methods.

Categories

Resources