Accessing Android resources

Android has a special folder called res, it make easily to accessing android resources. Using resources you can provide different characteristics to your application without modifying your code. Different dimensions, layouts, image sizes, languages, etc…

Accessing Android resources

Resource Types

The resource folder supports some special subdirectories.

DirectoryResource Type
animatorXML files that define property animations
animXML files that define tween animations
colorXML files that define a static list of colors
drawableBitmap files (.png, .9.png, .jpg, .gif) or XML files
layoutXML files that define a user interface layout
menuXML files that define application menus
rawArbitrary files to save in their raw form
valuesXML files that contain simple values
xmlArbitrary XML files that can be read at runtime

See more in Android website

When you put a file in a resource folder, the ide will automatic add it to a special class named R (This class is auto generated. NEVER change it). By default it will create something like (without extension), except for the files inside the folders values and color. For those the IDE will use the tags inside this file. For example:

In this case the IDE creates three entries on the R file: R.string.app_name, R.array.gender and R.dimen.app_padding



For accessing resources inside your XML you can use: @<resource_type>/<resource_name>


For accessing resources inside your classes you can use: R.<resource_type>.<resource_name>
Some methods just expect a Resource Id to be provided, like:

Sometimes you need to get the content of your resource. Context and Activity have methods to access resources

For more details take a look at Android documentation page Accessing Resources or in this example app

Add comment

By Daniel Passos

Daniel Passos

Android tech leader at Red Hat, Open source addicted, geek, teacher, traveler, Android and iOS freak, DJ in spare time and bodybuilding lover

Get in touch