Accessing application resources

  • Written by  Clive

Using your application resources

Application Resources icon

You should keep your resources, like images, strings, values, etc. separate from your code.

Application Resources folders

Some of the folders in the res directory

Put them where you can find them

Put your resources in the correct folder in the res/ directory. The directory names are important as this is where the system expects to find the resources.

These are the folders that you can use:

  • animator/ – files defining property animations
  • anim/ - files that define tween animations
  • color/ - files that define a state list of colors
  • drawable/ - image files
  • layout/ - files defining the User Interface layout
  • menu/ - files that define the app’s menus
  • raw/ - files in their raw form
  • values/ - files containing simple values, like strings, dimensions and colors
  • xml/ - any other xml files

Resources in these folders are given a Resource ID by the system. This is the resource ID that we use to retrieve the resource.

What’s it made of?

The resource ID is composed of:

  • resource type – for example, a string or drawable
  • resource name – either the filename excluding the extension or the value in the xml (android:name) attribute if the resource is a simple value like a string

Here’s an example:

The ic_launcher.png image file can be found in the res/drawable-mdpi folder. Its resource id is made up as follows:

  • resource type – drawable
  • resource name – ic_launcher
  • resource ID – R.drawable.ic_launcher

Keep the original

Only resources in the res/ folders have a resource id that you can use to access them.

The assets folder

You can save original files in the assets folder. They will not be compiled into the app. They won’t have a resource id but you can use the file system and the AssetManager class to access and read them.

You may be interested in reading, Using Android’s file system for saving application data: Part 1. Saving files and Part 2. Reading files.

The raw folder

You can also save original files in the res/raw folder. These files are given a resource id which you can use to access them.

Accessing your resources

There are two ways to access your resources:

  • in code – for example, R.string.my_string
  • in xml – for example, @string/my_string

Get them in your code

You can reference the resource in code by passing the resource id as a method parameter, for example:

textView.setText(R.string.my_string);

You can also get an individual resource by using Context.getResources()), (where Context is the application context),for example:

String myString = this.getResources().getString(R.string.my_string);

Or

String myString = getResources().getString(R.string.my_string);

So how’s the id structured?

The resource id is structured like this:

[<package_name>.]R.<resource_type>.<resource_name>

Where:

  • <package_name> - name of package where the resource is located (only include it if the resource is in another package)
  • <resource_type> - the R subclass for the resource, for example, string
  • <resource_name> - the resource filename without the extension or the android:name attribute value in the xml element, for example, my_string

Get them in your xml

Here’s an example of how you reference resources in xml:

android:text="@string/my_string"

So how’s the id structured?

@[<package_name>:]<resource_type>/<resource_name>

Where:

  • <package_name> - the name of the package where the resource is located (only include it if the resource is in another package)
  • <resource_type>R subclass for the resource, for example, string
  • <resource_name> - resource filename without the extension or the android:name attribute value in the xml element, for example, my_string

Referencing style attributes

You can reference the values of attributes in the current theme to style your User Interface to match that of the theme.

So how’s the id structured?

Just replace the @ with ?

Here’s the structure:

?[<package_name>:][<resource_type>/]<resource_name>

Where:

  • <package_name> - the name of the package where the resource is located. This would be android. You must include it
  • <resource_type>R subclass for the resource, for example, attr
  • <resource_name> - resource filename without the extension or the android:name attribute value in the xml element, for example, textAppearanceLarge

Here’s an example

android:textAppearance="?android:attr/textAppearanceLarge"

The system expects an attribute resource in this context so you can leave out the resource type, attr. The reference would then look like this:

android:textAppearance="?android:textAppearanceLarge"

Here’s a link to the attribute constants in the documentation.

Referencing system resources

Android has a number of strings, images, layout, etc. that you can use.

You can use these system resources by including the package name when you reference them. For example:

android:text="@android:string/cancel"

Check out the available system resources here, R class

And in code, use the android package name before the R class.

Here’s an example:

Android system resources layout

You can see a list of available layouts here, R.layout

Here’s a quick sample

I’ve created my own resource file and saved it in the res/values folder as my_resources.xml. It contains these resources:

Android resources

Declare the resources in a resource file

Then in my app’s layout file I reference these resources like this:

Referencing resources in xml

Reference the resources in your xml layout file

You can also do it in code like this:

Referencing resources in code

Reference the resources in your code

This is what it looks like when we run the app:

Android application resources screenshot

Our text view using our resources

This eBook may interest you, Android for beginners tutorials: Application resources bundle

I hope that you have found this tutorial helpful.