How Android Determines Which Resources to Pick Up

Learn the fundamentals of how resources work on the Android platform and understand how to create resources by working through a sample application.

Introduction

It is a bad idea to mix content with code, and resources are no different. The application that you first created for the US market might become the tomorrow's bestseller. If you haven't decoupled resources from code, you will not be able to sell to other markets until you have localized it, and will have to pay the tax to extract the resources and re-architect the code base. Why not start with the right way to put resources in your Android applications?

The Basics

In an Android system, resources are accessed by the identifiers, called resource IDs, that are generated in a project's R class. Depending on the type of Android system resources, they would be stored at different locations in the project folder hierarchy. A typical folder structure is as follows:

/values/: Various values in XML format. One difference from other folders is that content in XML files in other other folders have one type of content, whereas the XML file in /values/ folder can have values for different content types.

Resources to Enhance the User Experience

Android supports the ability to provide alternate resources to give users a differentiated experience from the default experience. These alternate resources can be provided for one or more of the categories listed above (under the resource folder list).

For example, if app developers want to provide richer graphics for HDPI devices, they can choose to offer these resources under the "/drawable-hdpi/" folder and provide the default resources under the "/drawable/" folder.

As evident from the folder name, the "hdpi" annotation to the folder name is interpreted by the Android platform to be lighted up only when the device meets the specification. For a normal device, the resources from /drawable/ folder will be picked. However, for a device with high-density screen, the resources will be picked from the /drawable-hdpi/ folder, if one exists.

There are a number of qualifier that can be used for resources. These qualifiers are listed below.

MCC and MNC (mobile country code and mobile network code): These resources are picked up from the SIM card. These are used by operating systems to show the network provider name in the Phone settings.

Layout direction (for example, ldrtl or ldltr): This represents the layout direction. The ldrtl is used for layouts which are from right to left (like Arabic regions).

Smallest width (format sw<N>dp): This represents the fundamental size of the screen irrespective of orientation. This is used for layout. For example, an application can provide different layout resources for a screen which sports a minimum of 600 pixels (for example, /layout-sw600dp/).

Available width (format: w<N>dp): This represents the minimum available width accounting for orientation.

Available height (format: h<N>dp): This represents the minimum available height accounting for orientation.

Screen size (values: small/normal/large/xlarge): This represents the size of the screen,

Alternate resource folders aren't nested; they are siblings of the main resource folders.

Multiple values for qualifiers are not allowed in a resource folder.

Hands On

Let's create a simple Android application that demonstrates how to provide resources.

Fire up Android Studio and Start a new Android Studio Project.

Figure 1: Starting a new Android Studio project

Provide ProvidingResourcesDemo as the Application Name and Click Next.

Figure 2: Providing an application name

On the next screen, leave the default values and click Next.

Figure 3: Leaving the default values in place

On the "Add an activity to Mobile" page, choose "Blank Activity". This creates an application with a single activity.

Figure 4: Adding an activity to the Mobile page

We are then prompted to customize the activity. We will leave the default values unchanged.

Figure 5: Again, leaving the default values unchanged

Click Finish to creating the project files.

You can observe the layout of the resources in the Project view.

Figure 6: Observing the resources' layout

We will now add alternate resources for layout when the orientation is portrait. Right-click the "Res" folder and select New -> Android Resource Directory.

Figure 7: Creating a new resource file

Figure 8:

This will create a folder called /layout-port/ under /res. Copy the activity_main.xml file from the /layout/ folder into this folder. Next, open the /layout-port/activity_main.xml file and make the following changes:

Note that string/hello_world_port does not exist yet. So, you will see an error if you compile the application now.

We now create another resource directory for alternate layout resources for landscape mode. Following similar steps as above and choosing Landscape orientation, we will now have a /layout-land/ folder under /res.

Figure 9: Making a Landscape directory

Copy the activity_main.xml file from the /layout/ folder into this folder. Next, open the /layout-land/activity_main.xml file and make the following changes:

Summary

In this article, we learned thee fundamentals of how resources work on the Android platform and we built a sample application that illustrates working with alternate resources. You can download the same code from the link at the end of this article.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.