Android App Development – Using Android resources part 2: Image, Color, Arrays and Dimensions Resources

On January 12, 2011

Image Resources:

Android provides us with the ability to use image resources in our applications. We can put image files in res/drawable directory and access them from the xml layout or by the generated ID from class file.

You can use image files of the following formats:

  • PNG
  • JPEG
  • GIF

we have three drawable folders:

  1. drawable-hdpi.
  2. drawable-mdpi
  3. drawable-ldpi.

These folders are used to put your images in to adapt to different screen sizes. For example you may create two files of the same image, one for high density screens (hdpi) and the other with smaller resolution for less dense screens (mdpi or ldpi).

Note that there are two restrictions:

  • If you use two files with the same base name you will receive an error. You cannot use two files like Hello.jpeg and Hello.png.
  • If you create any subdirectory under res/drawable directory any image files in it will be ignored.

Now let’s demonstrate how can we use image resources.

We will place an image called android.jpeg in te res/drawable directory and use it from xml layout as this:

<imageview android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/android" />

Or from code like this

ImageView img=(ImageView)findViewById(;

Or like this to get the image as a drawable object

ImageView img2=(ImageView)findViewById(;
Drawable drawable=this.getResources().getDrawable(;

Color drawable Resources:

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

You can define the color drawable resources in values/strings.xml file or by creating a custom xml file to hold these resources.

Define color drawable resources like this:

<drawable name="redBox">#f00</drawable>

Or from code like this:

TextView txt=(TextView)findViewById(;

Or like this:

ColorDrawable drawable2=(ColorDrawable)this.getResources().getDrawable(R.drawable.redBox);

And finally like this:


Notice that if the textview does not have text the background color will not appear.

Color Resources:

Color resources are defined as string resources in res/values directory
You can define XML files that contain definitions for colors that can be used in your application.

Colors in Android are hexadecimal RGB values, also optionally specifying an alpha channel (transparency).

You have your choice of single-character hex values or double-character hex values, leaving you with four styles:

  • #RGB (Red: #F00).
  • #ARGB (Red with Alpha 5: #5F00).
  • #RRGGBB (Red : #FF0000).
  • #AARRGGBB (Red with Alpha 50: 50FF0000 #).

You define colors in the xml file as follows:

<color name="Red">#FF0000</color>

Now you can use it from code like this:

TextView txtColor=(TextView)findViewById(;

Or from the layout as this:

<textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="This is a color string from layout" android:textcolor="@color/Red" />

Dimensions Resources:

You can define Dimension resources to use them with your widgets to define padding, width or height.

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

To define dimension resources in xml files you can write it like this:

<dimen name="PixelDim">10px</dimen>
<dimen name="PointsDim">10pt</dimen>
<dimen name="InchesDim">0.2in</dimen>
<dimen name="MilliDim">5mm</dimen>
<dimen name="DensityDim">20dp</dimen>
<dimen name="ScaleDim">20sp</dimen>

And you can use them from the xml layout definition like this

<textview android:id="@+id/txtInches" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Font size is 0.2 Inches" android:textsize="@dimen/InchesDim" />

Or from code like this:

TextView txtdp=(TextView)findViewById(;

Array Resources:

Array resources allow you to define custom string arrays that hold values you can use in your application such as a countries list or a list of names.

An Array resource is defined using string-array element while items are defined using item element.

<string-array name="countries">

and you can use them from code like this:

String [] Countries=this.getResources().getStringArray(R.array.countries);
You can define more than one string-array in the same file.
That was it for Android Resources. Please ask any questions you may have in the comments and don’t forget to check next week for another Android development tutorial.