Handling Multiple Resource Sets, Part One

Welcome to the new “Building ‘Droids” section of AndroidGuys! This column looks at Android from the developer’s perspective, poking around dusty corners of the Android SDK, and showing how you can create interesting and effective Android applications.

Today, we’ll talk about how to have your Android application support multiple sets of resources for different scenarios, such as internationalization (i18n) and localization (l10n). This material is adapted from a portion of The Busy Coder’s Guide to Android Development.

Today’s material is for intermediate Android developers — some experience with the Android SDK is expected. And today’s material is targeted for the M5 SDK, so if you’re reading this in the archives, bear in mind that the SDK may have shifted since this post was written.

The most commonly encountered scenario for needing multiple sets of resources is i18n and l10n, as noted above — if you want to support English and Spanish in one application, you will need strings for each. However, that is not the only scenario:

  • Screen orientation: is the screen in a portrait orientation? Landscape? Is the screen square and, therefore, does not really have an orientation?
  • Screen size: how many pixels does the screen have, so you can size your resources accordingly (e.g., large versus small icons)?
  • Touchscreen: does the device have a touchscreen? If so, is the touchscreen set up to be used with a stylus or a finger?
  • Keyboard: what keyboard does the user have (QWERTY, numeric, neither), either now or as an option?
  • Other input: does the device have some other form of input, like a directional pad or click-wheel?

The way Android presently handles this is by having multiple resource directories, with the criteria for each embedded in their names.

Suppose, for example, you want to support strings in both English and Spanish. Normally, for a single-language setup, you would put your strings in a file named res/values/strings.xml. To support both English and Spanish, you would create two folders, res/values-en and res/values-es, where the value after the hyphen is the ISO 639-1 two-letter code for the language you want. Your English-language strings would go in res/values-en/strings.xml and the Spanish ones in res/values-es/strings.xml. Android will choose the proper file based on the user’s device settings.

Seems easy, right?

For just one criteria, such as language, having multiple resource sets is easy. Things get a lot more tricky when you need to handle several different axes of change, such as different languages and different screen sizes. In the next installment, we’ll see how to handle these more complex scenarios.

About author

AndroidGuys 4626 posts

Founded on November 5, 2007, we've enjoyed bringing you the latest in Android news and rumors. Updated daily, we strive to deliver reviews, opinions, and updates on all things related to Android.

You might also like

News and Rumors

CyanogenMod Support Now Official for 3 Samsung Galaxy S Devices

Its official!  Now everyone can relax as CM7 has arrived on the popular Samsung Galaxy S series handset.  Its been a long time coming, divorces and ruined friendships are simply

News and Rumors

Madden 12 Comes to Android with Real Teams, Players

Headed out the door to pick up your copy of Madden 12 for your favorite gaming console today?  What did you do when you ran into a mass of guys

News and Rumors

Google Keep updated to include reminders and Google Now integration

Google announced on Wednesday that a moderate update has arrived for Google Keep, adding in a number of noteworthy enhancements. Details include the ability to set reminders for certain times