Monday, September 8, 2014

Starting From Scratch: Android - Resources

This week we're continuing the Starting From Scratch series. Today we're going to take a look at Android resources. There are many types of resources in Android. First i'm going to outline the different resource types. Then I'll explain what configuration qualifiers are and what they're used for. Finally I'll call out a few important configuration qualifiers that will be used in most applications.

All resources in your Android application are stored in the res/ folder of your project. Resources are grouped by type and configuration.

Resource Types


The easiest way to explain the different resource types is to simply list out the folder structure and what type of resource is provided in each folder. The following is a list of the various resource types.

res/
anim/Store tween animation XML files in this directory.
color/Store color XML files in this directory.
drawable/Store bitmaps (png, jpg, gif) files, or XML files for bitmapsframe animations, 9 patches, state lists, and shapes in this directory.
layout/Store layout XML files (UI composition) in this directory.
menu/Store menu XML files in this directory.
values/Store XML files for strings, styles, other value types in this directory.

Configuration Qualifiers


One of the most powerful things that you can do with Android resources is specify configuration qualifiers for the various resource types. Configuration qualifiers allow you to target specific resources towards a specific device, screen size, or etc. There are different configuration qualifiers depending on the context in which they are used.

Configuration qualifiers are appended to the resource type and separated with a dash (<resource type>-<configuration qualifier>). I won't go into detail on each qualifier as table 2 in the Android resource documentation provides an exhaustive list of all qualifiers. But I want to call out a few different qualifiers that are important to know.

Screen Pixel Density


Screen pixel density qualifiers are used with drawables and help you to avoid images that look terrible from being scaled up as well as providing lower resolution images for screens that are not very dense.  There are six types of screen pixel densities nodpi, tvdpi, ldpi, mdpi, hdpi, and xhdpi.

nodpi is for resources that you do not want scaled. tvdpi is for screens that fall in-between mdpi and hdpi (like televisions). ldpimdpihdpi, and xhdpi are for low, medium, high, and extra high density screens respectively.

For example, images in the res/drawable-mdpi/ folder should be images that are ideal on a medium density screen.

If a screen pixel density is not specified that's ideal for the device Android will scale up/down based on the best match density.

Language and Region


Language and region qualifiers use the ISO 639-1 language and an optional ISO 3166-1-alpha-2 region preceeded by a lowercase r. For example fr-rCA for Canadian French. Language and region qualifiers are the easiest way to localize your application and provide different text translations for different languages and regions.

Screen Size


Screen size qualifiers allow you to target screens using small, normal, large, and xlarge. When targeting Android versions prior to Honeycomb these are best way to provide layouts optimized to specific screen sizes. But, it's important to note that the screen size qualifier is not the ideal way to determine phone versus tablet. There are some tablets with lower screen sizes than some of the higher end phones. This is because the size relates to the density of the screen.

If you're providing an application that supports a version of Android prior to Honeycomb and you want to target screen size you should use a combination of screen size and the smallest width qualifier.

Smallest Width


The smallest width qualifier allows you to specify screen sizes with a minimum number of density independent pixels. The format us sw<N>dp. For example layout-sw600dp provides a layout for screens with a minimum of 600 density independent pixels.


In Oct 2013 I gave a presentation at the Big Android BBQ on Building For Multiple Screens. If you're interested in a deeper dive into the subject checkout my presentation slides.

No comments:

Post a Comment