• The resource system and qualifiers
  • Images
  • Audio

Resource System

  • Resources are placed in res and its subfolders
  • Resources are
    • Xml files compiled to a binary form
    • Image files
    • Other binary files, like mp3
  • Purpose
    • Automatically select a particular resource depending on some system condition

Resource Identifiers

  • Each resource corresponds to an identifier
    • Identifiers are Java integer constants
  • Custom resources
    • Generated in R class of project
  • Android built-in resources
    • Predefined in android.R class

Resource Qualifiers

  • Some condition or property in the system
    • Checked when resources is loaded by Android and defines what specific resource is chosen, if multiple choices are available
  • Subfolders of res with suffix
    • Suffix corresponds to a condition
    • Chosen if condition matches system
  • Subfolder of res without suffix
    • Chosen if no other condition/subfolder matches system

A few qualifiers

Use cases

  • Language of device
    • Put all user interface strings as resources and have them translated
  • Landscape/Portrait orientation of device
    • Distribute views differently for layout xml when device is in landscape orientation
  • Tablets
    • Add more views and controls when screen is larger

Create resources with qualifier

  • File, New, Other, Android, Android XML File
  • Resource Type
    • Layout, Values, Drawable...
  • File
    • Name of resource, can exist for other qualifier
  • Root Element
    • Depending on Resource Type
  • Chosen Qualifiers
    • Select one (or more) qualifiers from the Available Qualifiers
    • Specify value of qualifier (if value exists)

Resource strings with language qualifiers

  • Recommended for all user interface text
    • Development environment have quick fixes commands to help you
  • Stored as resource xml files in /res/values/strings.xml
      <string name="msg">Welcome to this application!</string>
  • Reference in layout xml files
    <TextView android:text="@string/msg" />
  • Reference in Java


  • Abstraction for painting a rectangular area
  • Xml files
    • Shapes
    • Selectors
    • Layers
  • Image files
    • Bitmap - image files like jpg and png
    • 9-patch image
      • Image file with stretchable parts

Bitmap drawables

  • Create "drawable" folder below "res" for general purpose images (no qualifier)
    • Drag and drop image files on folder in Eclipse to add it to project
  • File name must comply with Java naming rules
    • Keep it to a-z, 0-9 and underscores
  • File name is also the resource identifier
  • /res/drawable/background.jpg
    <ImageView android:src="@drawable/background"


  • android:src="@drawable/icon"
    • The drawable to display
    • Can be an image, or some other drawable
  • android:scaleType
    • Defines how an image is scaled inside the view
    • fitXY - scale to exact size of view
    • centerCrop - scale uniformly to max and crop the rest
    • centerInside - scale uniformly without cropping

Images in TextView, Button

  • TextView and all derived views like Button supports drawable next to text
  • android:drawableLeft
    • The drawable to display to the left of the text
    • drawableTop, drawableRight, drawableBottom
  • android:drawablePadding
    • Space between text and drawable

Bitmap in resource xml file

  • Create resource xml file and reference bitmap image
    • Define tiling options for backgrounds
  • Layer resource
    <bitmap android:src="@drawable/imagefile"

9-patch image

  • Suitable for buttons
  • Makes bitmap images look good on buttons regardless of size
  • Resource png image

Icon and image sources


Play audio resource

  • Add audio file(s) as resources
  • Start playing in Java code (will continue until end)
    MediaPlayer player = MediaPlayer.create(this, R.raw.soundfile);
  • Stop playing before end