Smartphones

Android resource types: from basic to advanced

App developers, read this overview of some of the basic and more advanced resource types that are commonly used in Android projects.

Anything beyond the simplest of Android apps will use resources -- text, sizes, images, layouts, and so forth. The Android App Resources system allows you to build flexible and reusable components for your app and simplifies the job of handling different device configurations.

Within an Android project, resources are stored under /res. Sub-directories can be created for different resource types and configurations (including versions for various languages and locales). However, beyond the basic strings and layouts, many tutorials don't delve into the more complex resource types. Let's take a look at some of the types of resources that Android can handle.

Basic Android resource types

String resources store a string of text. A string resource can be accessed from code as well as XML layouts, so moving strings into a resource file allows you to manage all text in one location. Using string resources for all text that users will see is considered a best practice, especially when an app will be localized. Most apps will also have layouts. This is the XML description of the graphical layout for a view, a fragment, or an activity or a piece of one of those. Layout resources are powerful tools to handle multiple screen configurations, such as tablet and phone screen sizes. All apps will have at least one set of drawable resources: the launcher icon. The drawable resource directory can contain not just images but also selectors and other XML UI components. Menus are another common resource type. Menus populate an entire options menu, a context menu, or a submenu directly from the resources; this allows sharing a menu between different activities and saves the need to create each menu item in code. Colors are a named color value with or without an alpha component. Defining colors can make layouts and other resources much easier to read for those of us who do not have the hex values for colors memorized.

The above resources are commonly used, so you are likely to have encountered them in sample projects and tutorials even if you have not used them in your projects yet. There's much more to the resource system, though.

More advanced resource types

Once you have all the basic resource types in your app, you may want to delve a little deeper.

Let's start with dimensions. This powerful tool allows you to use the same layout file for multiple densities or screen sizes, requiring only the dimensions XML file to be unique for each size. For example, this line in a layout file is controlled by the dimension resource:
android:layout_height="@dimen/widget_height"

In this case, the dimensions XML could specify:

<dimen name="widget_height">60dp</dimen>
Arrays are also very handy. Android handles integer arrays and typed arrays. The former contains integers, while typed arrays allow you to create arrays of any other type of resource that you define, such as drawables.  Arrays can be a very handy way to define a set of data that can be easily switched (e.g., replacing test data with production data or activating paid content). Here is an example.
<integer-array name="array1">

<item>1</item>

<item>2</item>

<item>3</item>

</integer-array>
The ID for an integer array is also an integer, so you can create arrays of arrays if needed.
<integer-array name="array2">

<item>@array/array1</item>

</integer-array>

Even more power and flexibility

As the infomercials say, "but wait, there's more!" You can:

  • Create reusable styles to apply to a view or an activity
  • Create entire themes for an app
  • Store a color state list for a selector
  • Store primitive types such as integer, bool, and ID
  • Define animations

Going beyond that, you can create your own types of values, as well as create references to other resources.  This allows a great deal of flexibility and power, but it can also take quite a bit of work to figure out -- that's partly because the most advanced features don't seem to have much documentation and require sifting through the example code.

Make use of resources efficiently

Android will automatically pick the appropriate resource directory based on orientation, screen size, version of Android, and other factors. If you make sure your resources are in the appropriate locations, it will make your life easier. You should utilize the built-in conditional inclusion -- don't fight it!

In addition, the resource layering system in Android means that any resource that is redefined in a child project will hide the value in a parent library project. This can be a handy way to override strings, dimensions, or layout files in child projects.

With all of these resources available, go make your Android app easier to maintain and more responsive to different device configurations.

Adding Android resources in Eclipse

About

Tim Mackenzie, author of the Android Income Series books, is a software engineer that escaped the cubicle world at a large company to go solo with Android app development. He uses this freedom to teach others how to make money with Android apps. Visi...

0 comments

Editor's Picks