Intro to Android

Chapter 5: Resource System

Meier puts it well, "using layout resources decouples your presentation layer from the application logic, providing the flexibility to change the presentation without changing code. This makes it possible to specify different layouts optimized for different hardware configurations, even changing them at run time based on hardware changes (such as screen orientation changes)."

I've seen a too many duplicated layouts, hard coded values, and hard coded Java views cluttering Android applications! All of these practices lead to difficult to maintain apps and makes it harder to scale to newer devices and form factors. However, if you leverage Android system from the start and let it do the heaving lifting for you, you can avoid a lot of pain down the road.

Although I find XML a pain to read and write, I've grown to love the resource system. It is one of the most powerful tools you have when constructing your application, but is often misunderstood and definitely underrated and underutilized.

res/

You place your layouts, drawables, and values (e.g. strings, colors, dimensions) in this folder.

The basic folder structure looks like this:

res/
    drawable/
    values/
    layout/

One annoying thing about these folders is that you can't create subfolders. This is especially annoying in the layout folder since you end up with lots of views in your system. You can however, make up your own file names and group resources that belong together.

There's lots of great information on Android's site about how to suport mutiple form factors: http://developer.android.com/guide/practices/screens_support.html

Resources

  • Meier, Reto (2012). Professional Android 4 Application Development (Loc.2816-2818). John Wiley and Sons. Kindle Edition.