What is the concept behind R.java? What is the concept behind R.java? android android

What is the concept behind R.java?


Biggest advantage is in Localization and Providing alternate resources for different screen sizes.

e.g you can have a String resource R.string.myname this could be a defined in english in /values-en/strings.xml and in spanish in /values-es/strings.xml

System will take care or picking up the right file depending on the locale you just need to use @string/myname in your layout file or R.string.myname in your code.

Similarly you could have two layout files for portrait and landscape defined in

res/layout/mylayout.xmlres/layout-land/mylayout.xml

In your code you will just specify R.layout.mylayout to inflate the layout. Resource manager picks up the file in layout-land if the device is in landscape mode.

Doing this manually would be a nightmare -- hence the need for R file


android.R.java is not just where XML ids are stored. It also contains access to resources - such as drawables, layouts, strings, arrays, and basically anything you can declare in resources.

Personally I find that it is useful when using Eclipse. I can simply type findViewById(R.id. and Eclipse will show a tooltip with a list of options to choose from.

However at a platform level, I would say that the hardcoded id variables help prevent errors when using Strings to identify resources -- something that can be debuggable while programming (or during compilation, rather than runtime).


The comparison feels somewhat a little bit (actually) weird, because you compare two mechanism based on the fact that they use named things for doing stuff. For resource loading, e.g., have a look at how resource handling is done in the .Net world.


It provides for compile-time checking if the resource is available. Because if it not, there will not be a static inside R.java that points to it. In the Spring example, how can you be sure there is a bean called beanId? It does not provide for checking if it is the right type of resource, though.

Why is this not loose? As long as the new resource has the same name, it will generate the same static constant. In Spring, you would have to use the same bean name.

Design Pattern? None. It just adds one level of indirection by naming the resources and then refer to them only by name, not by directly loading them from their true location.

Actually, the resources are injected, because resource loading must cope with localization. See here for how Android does stuff; in the .Net world, additional cultures are packed into satelite assemblies; the resource manager will load the right one based on the current culture.