Handling Application Resources


Resources play a key role in Android Architecture. A resource in Android is a file (like a music file)or a value(like the title of a dialog box) that is bound to an executable application. These files and values are bound to the executable code so that we can change them without need of recompiling the application.

Examples of resources include strings, colors, and bitmaps.
We can create and store our resource files under the appropriate subdirectory “res/” directory in our project. Android has a resource compiler (AAPT-Android Asset Packaging Tool) that compiles resources according to which subfolder they are in, and the format of the file.

Why We need Resources :         

  • The resources are bound to the application so that we can change them without needing to change the source code or recompile the application.
  • The Android Generates an ID for each resource file so we can access them directly and easily in our java code. All the resources IDs are added to the R.Java file.
  • Using resources is very useful in Localization and Internationalization of the application while develop multilingual applications.
  • Resources includes not just the labels but it can include alignment, directions images or any kind of files.

Resources class :

  • Class for accessing an application’s resources, defined in android.content.res.* package.
  • The Android resource system refers all non-code assets associated with an application, like images, audio, video ..etc. We can use this class to access our application’s resources. We can generally acquire the Resourcesinstance associated with our application with getResources().

Method :

Configuration Class :

  • This class describes all device configuration information like as input modes, screen size and screen orientation.
  • We can get this object fromResources, using getConfiguration(),from an activity, we can get it by chaining the request with getResources():

              Fields :


Example : Get Current Screen Orientation in ANDROID

The following method does the required functionality.

Strings, colors, arrays, dimensions. Defined in res/values directory. Using them is very useful in Localization and Internationalization.Types of Resources

  • Images put in res/drawable directory.  We can put all the images or icons we need in our application.
  • Animations, defined in res/anim directory. We can define animations.
  • XML, defined in res/xml directory for xml files containing our custom data.
  • Layout resources, defined in res/layout for declaring the views that construct the user interface of the activities.
Resource Type Location Description
Colors /res/values/any-file Represents Color identifiers pointing to color codes. Can access using R.color.*

The XML Node in the file is /resources/<color>

Strings /res/values/any-file Represents String resources. Can access using R.string.*. The XML Node in the file is /resources/<string>
String arrays /res/values/any-file Represents a resource that is an array of strings. Can access using R.array.*. The XML Node in the file is /resources/<string-array>
Dimensions /res/values/any-file Represents dimensions or sizes of various elements or views in Android. Supports pixels, inches, millimeter, density independent pixels, and scale independent pixels. Can access using R.dimen.*. The XML Node in the file is /resources/<dimen>
Images /res/drawable/multiple-files Represents image resources. Supports images include .jpg, .gif, .png etc. Can access usingChennai H O R.drawable.*
Arbitrary XML files /res/xml/*.xml Android allows arbitrary XML files are resources. These files will be compiled by AAPT compiler. Can access using R.xml.*
Arbitrary raw resources /res/raw/*.* Allows arbitrary non compiled binary or text files under this directory like .txt, audio or video files. Can access R.raw.*

Working with string resources

Single Value String values:

 Accessing String values in layout files (for Ex in main.xml)

String Array Values:

  Accessing from java code

Working with Boolean resources
  <bool> is the tag used to Boolean values in xml file, values may be true/false


Accessing from layout xml file (for Ex : main.xml)

Accessing from Java Code :

Working with integer resources
Used to define Integer constants in xml file we can define in two ways, are
Single Value Integers :
XML file saved at res/values/integers.xml:

  Accessing from java code :

Integer Array :

XML file saved at res/values/integers.xml:

 Accessing from java code :

Working with color resources


 Applying color to textview in layout file (for Ex main.xml)

 Getting color in java code

Working with Dimension Resources :

              Pixels, inches, and points are all examples of dimensions that can play a part in XML layouts or Java Code.
The dimension resources can be defined in the following units:

  • Pixels: (px).
  • Inches: (in).
  • Millimeters: (mm).
  • Points: (pt).
  • Density: (dp) density-independent pixels based on 160 dpi (dot per inch).
  •  Scale: (sp) Scale-independent pixels (dimensions that allow for user sizing; helpful for use in font sizes, image sizes..etc).


Accessing from layout xml file(for Ex main.xml file)

 Getting dimension in java code :

Working with image resources

  1. Android generates resource IDs for image files placed in the /res/drawable subdirectory. The supported image types include .gif, .jpg, and .png. Each image file in this directory generates a unique ID from its base file name. If the image file name is sample_image.jpg, for example, then the resource ID generated will be R.drawable.sample_image.
  2. We have four types of drawable folders:
  • Low density screens (ldpi): 36x36px, 120dpi
  • Medium density screens (mdpi): 48x48px, 160dpi
  • High density screens (hdpi): 72x72px, 240dpi
  • Extra high density screens (xdpi): 96x96px, 320dpi
  1. These folders are used to put our images in to adapt to different screen sizes. For example we may create two files of the same image, one for high density screens (hdpi) and the other with smaller resolution for less density screens (mdpi or ldpi).

  Accessing images in layout xml file (for Ex main.xml):

 Accessing from Java code :

Working with color drawable resources
              We can define XML files that contain definitions for color drawable resources which are color rectangles that can be used as backgrounds.


  Java Code to Access :

Example on Color Resources :
Define an Android project, add a new Resource XML file in values folder, and modify the main.xml, run the project, observe the output./res/values/mycolor.xml


Leave a Reply