View RSS Feed

Android Developer

Referencing Application Resources

Rate this Entry
by , 02-11-2012 at 10:53 AM (796 Views)
All the application resources are compiled at build time and are stored in “res” project directory structure. These resources can also be used programmatically. Also they can refer to other resources in application. These application resources are accessed using class file R.java programmatically. Get the application’s resource object and call getResource() method to reference a resource in your activity class. After this call the appropriate method to access your desired resource.

For example, use below code to get a string “hello” that is defined in strings.xml file.

Java Code:
String greeting = getResources().getString(R.String.hello);
Use the following code format to reference an application’s resource from another resource like a layout file etc.
@[resource type]/[resource name]
For example, the string used in above code is referenced as follows:
@string/hello

Submit "Referencing Application Resources" to Facebook Submit "Referencing Application Resources" to Digg Submit "Referencing Application Resources" to del.icio.us Submit "Referencing Application Resources" to StumbleUpon Submit "Referencing Application Resources" to Google

Categories
Android Resource

Comments