Wednesday, 13 May 2015

Android drawables


1.      What is an android drawable?
2.      What are the types of drawables?
3.      How to access drawables via xml and code
4.      3 ways to instantiate drawables
5.      Android system variables(public and private)
6.      User created xml variables
7.      Drawables vs images

What is an android drawable
 Basically, a drawable  is anything that can be drawn to the android screen and you will be storing them in res/drawable folder.

All images in an android application are drawables.

Other drawables are rather described to android inform of geometrical properties (size,gradient,color etc) in xml and gives an effect on the ui similar to an image.

What are the types of drawables?

Drawables can fall in any of the following categories:

  1. Bitmap File
  2. Nine-Patch File
  3. Layer List
  4. State List Level List
  5. Transition Drawable
  6. Inset Drawable
  7. Clip Drawable
  8. Scale Drawable
  9. Shape Drawable etc.

After this post I will be covering most of these categories one at a time full with code examples.

Accessing android drawables
A drawable is mostly used as background image or icon for a widget/view. It can be accessed from either xml resource file or in java code:

In xml, using icon or background property like so:

In code like so:


We eliminate the file extension because at run time, the file names become id’s for the resource, so my_image.png becomes my_image, so goes for the xml’s.

3 ways to instantiate a drawable

·        Using an image found in your drawable resource folders
·        Using an xml file that defines the drawable properties to android, still located in res/drawables
·        Using normal class constructors totally in code

We shall see details of these methods in later posts with examples.

Android system drawables

Android provides many inbuilt drawables that are used in the apps that come with the system. most of these drawables are available to the programmer to use in his apps.

That way you can achieve a consistent look with the  system apps.

Some are public and others, private. what I know is using a private drawable in your code causes error, this is to protect the developer and his app because any resource that is not in the public SDK is subject to extensive change or even removal in subsequent versions.

However if you really need to use these drawable resources, its safest to copy the image files from the android source into your own drawable directory.

They are located in res\drawable-mdpi in the android jar when you un-jar it.

Xml drawables
Drawables have a lot of potential to make your apps visually stunning, if you only know how to use them well, I must say they are very much under used by developers.

Background resources for views such as buttons, menu bars, tool bars and even the activity backgrounds use drawables extensively.

All the drawable types we saw above are available to you to define in xml and use in your apps.

Why use drawables
Android is so popular that it runs on a large multitude of devices with varying screen size. 
When you use images for background rendering, you may have to provide upto 4 versions of the same image drawable to take care of the different device screens. 

This image from the developer page should say it all

On the contrary,  drawables can adjust themselves to fit any screen size, so you only have to define it once.

Even if the drawable you want to use is an image, android has a way you can wrap it around an object in xml, such that at run time the image.png remains opaque. We shall see this in later posts.

No comments:

Post a Comment