Siavash
Siavash

Reputation: 7853

Concept behind the Java resources, striking gs, "R.id"

I ran into an issue that made me realize there is core concept I'm not getting. For some reason when I went to add a value to my strings.xml file, something happened and my xml file was giving a parsing error. The only way I was able to fix this was by deleting the strings file and re-creating it by right clicking on the values folder, and going to "add android xml file" and naming it strings.xml. This made the error go away, but now, my activities are complaining and saying they can't resolve R (as in R.id).

This made curious as to what's the relationship between R and the xml files. And why is that when you reference a string, you use @String/something, when the file that contains it is called strings?

Finally why is it that even though I created an xml file called strings.xml, in the right location, my project doesn't see his as the THE strings.xml file?

Upvotes: 2

Views: 162

Answers (3)

Raghav Sood
Raghav Sood

Reputation: 82553

Names

The R resources are organized in a specific manner:

  • R.layout.* - These resources are your layout files, which you have created in the various layout-* folders
  • R.menu.* - These are the resources which are your menu files, created in your various menu-* folders.
  • R.id.* - These are the resource identifiers for your various Views and Menu items etc. There can be more than one element with the same ID, as long as they're in separate files.
  • R.string.* - These are your string resources, declared in <string> elements in files in your values-* folders. The filename is irrelevant. There can only be one string element with a particular name in a single folder. So you cannot have two hello strings in values, but you can have one each in values and values-en.
  • R.drawable.* - These are the various XML and image drawables you have in your drawable-* folders.

There are many more, but these are the most common. I'll add the rest when I have a bit more time on my hands.

After compilation

During compilation, the Android build system puts all XML files into a binary format. Everything except the /res/raw is compressed as well. This is particularly helpful with layouts, which can be inflated faster after compression. Individual files cease to exist after compilation, and everything becomes one big binary file. You access the data in this file using R.<resource_type>.<name> from Java, and @<resource_type>/<name> from XML

Naming

For some resources (drawables, layouts, menus) the filename matters, as the resource is referred to using that. For other resources (strings, colors, styles), the filename is irrelevant, as long as you use the correct element (for example <string> for Strings) when declaring them within the file.

Upvotes: 2

Karakuri
Karakuri

Reputation: 38595

When your project gets compiled and built, one of the things that happens is the generation of the R file. Each named resource is given a public static final int identifier, like R.string.foo, which would reference a string defined with name="foo". This way you can reference a resource from Java and Android will map it to a resource and load it from your application package.

Resources can be overloaded; you could have two strings both named foo that are qualified for different configurations, but they have the same identifier in the R class. The system will choose the one that best matches the device configuration at run time.

If there is an error with any of your resource declarations, R will fail to build and any java classes that reference it will therefore complain about it. You can do Project > Clean in Eclipse and see if that helps, but otherwise make sure your resources are properly declared and/or have valid names.

Upvotes: 1

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

R.java is a seperate file created by eclipse, 
its not related to strings.xml ,Clean your project then build and run again . 
you will found R.java inside your gen folder
Read this

http://developer.android.com/tools/projects/index.html

Upvotes: -2

Related Questions