Resources Used for Android Application Development

Introduction

In the previous tutorial, we have discussed the concept of Adapter in Android, and we used the adapter for populating ListView, GridView, and Spinner. In this tutorial, you will know learn about Android application resources and their usage.

Resources

Resources are the additional supporting files or material used for building anything. It can be the static content or the additional code to access that static content. Here are some commonly used resources used for Android Development.

  • Images i.e. bitmaps, jpegs, etc.
  • Layouts i.e. XML files which define the look and feel of Android Activities.
  • User Interface String i.e. language qualifiers, text to be displayed.

Talking about Android resources, two questions may raise in your mind.

How and where to place resources in Android?
How to access these resources?

Provide Android Application Resources

To provide resources to Android application, you must have a separate externalize source so that you can keep your code, layouts, strings and images, separate from each other. This Externalizing will help you to manage your resources easily and independently. For alternate devices depending upon screen sizes and configurations, you must provide alternate resources. Android decides which resource to use at runtime, depending upon the configuration of the device. There are two main categories of Android application resources.

  1. Default Resources
  2. Alternate Resources

Default Resources

Default resources are the must use resources they are independent of platform, configurations and screen sizes. When there are no alternate resources which match the configurations, Android automatically uses default resources. You must provide default resources for your Android application so that your application can perform perfectly. After you provide default resources you can provide alternate resources for your Android application, as for specific configurations your Application can perform better.

Layout with Default Android Application Resources
Layout with Default Android Application Resources

Alternate resources 

Alternate Android application resources are the specially designed resources for specific configurations. To specify alternate resources, you should specify the configuration and configuration qualifier to the new directory name where you want to place alternate resources.

Layout with Alternate Android Application Resources
Layout with Alternate Android Application Resources

Place Resource Files

In your project’s resource res/ folder there is a hierarchy of subdirectories. These subdirectories have different names according to the resource type. These names are just to differentiate the resource types, but they have some supported names. Place the resources in the specific sub-directory accordingly.

Directory Structure for Android Application

Project

src/

MainActivity.java

res/

icon.png    

drawable/

app_icon.png  

layout/

activity_main.xml  

values/

strings.xml 

Note: If you place the resource directly under resource res/ folder, It will give a compile time error.

Supported Subdirectory Names

Android have some special names that you can use in resource res/ folder for subdirectories.

  1. animator/ used to define animations (XML files)
  2. anim/ used to define tween animations i.e. view animations (XML files)
  3. color/ used to define color properties (XML files)
  4. drawable/ used to define bitmap files or XML files which can be compiled to produce a drawable resource
  5. layout/ used to define UI components for Android Activity
  6. menu/ used to define menus i.e. Options Menus and context menus etc.
  7. raw/ used to store raw components
  8. values/ used to define variable values i.e. strings.xml, arrays.xml, colors.xml, dimensions.xml and styles.xml
  9. xml/ used to define other XML resources which can be used at run time

 

Alternate Resource for Specific Configuration

You should provide alternate Android application resources to support specific device configurations. To differentiate a resource from other default resources by appending appropriate configuration quantifier to subdirectory name.

<resources_name>‐<config_qualifier>

Example of Alternate Android Application Resource

res/

drawable/

icon.png

drawable-hdpi

icon.png

Rules for Alternate Android Application Resources

  • The alternate Android application resources must have the same name as it is of default resource. Android automatically detects the configurations and load the appropriate resource.
  • Android supports several configuration quantifiers, but you should have to place them in the appropriate directory. All similar directories have same file name but with additional quantifier separated by a dash (-).
  • Alternate resource directories can not be nested.

Android Configuration Qualifiers

Android uses configuration qualifiers which include

1. MNC and MCC

Country code MCC (Mobile Country Code) and network code MNC (Mobile Network Code) from the sim card. Here is a list of Pakistan’s country code and network code

  1. Mobilink (MNC: 01 MCC: 410)
  2. Ufone (MNC: 03 MCC: 410)
  3. Zong (MNC: 04 MCC: 410)
  4. Telenor (MNC: 06 MCC: 410)
  5. Warid (MNC: 07 MCC: 041)

2. Languages and Region for Configuration Qualifier

The language of Android device can be defined by the first two letters of the language also known as language code. These language codes are standardized by ISO 639-1. Region code is optional; you can add region code by appending the region code with preceding lowercase r.

These codes are not case sensitive, but you must specify the language code with the region code. prefix is used to distinguish the region portion of the qualifier.

Language Code: en,  fr

Region Code: en-rUS,  fr-rCA

3. Layout Direction Resource for Android Application

You can add the layout direction resource to by using shortcodes for layout direction i.e. ldrtl  (layout-direction-right-to-left) and ldltr  (layout-direction-left-to-right).

ldltr  (layout-direction-left-to-right) is the default layout direction resource.

4. Smallest Width Resource for Android Application

Smallest width is used when the smallest size of the screen is at least equal to the specified size of the screen. In this type of case, screen orientation remains the same even when the screen’s orientation changes. For example, if the smallest width resource is  sw320dp and the width of the device is equal to 320dp the specified resource will be loaded.

5. Available Width Resource for Android Application

Available width is used when you want to specify the minimum width which the resource should use. This configuration value changes when the screen orientation is changed i.e. landscape to portrait and portrait to landscape. Configuration value changes when the orientation change according to specified resource width. If the available resource is w720dp and the width of the device become equal to 720dp after the screen orientation is changed the configurations will load this resource.

6. Available Height Resource for Android Application

Available height is used when you want to tell Android system about the desired height of the screen and specifies the minimum available height in dp at which the resource should be used. This resource is also loaded when the configuration changes from landscape to portrait. If you specifies h720dp as the minimum available height and the height become equal to 720dp, the resource will be loaded.

7. Screen Size for Android Application

There are four types of screen sizes which are small, normal, large and xlarge.

small: 320×426 dp
normal: 320×470 dp
large: 480×640 dp
xlarge: 720×960 dp

8. Screen Aspect for Android Application

Screen aspects are the aspect ratio of the Android application, but it is not related to screen orientation. There are two types of screen aspects in Android applications i.e. long and notlong (a long screen is wider in size)

9. Screen Orientation for Android Application

Screen orientation is the landscape or the portrait of the screen type. This changes throughout the lifecycle of the Android application as when the user changes the orientation by rotating the device. In Android, you can use two types of screen orientation by using the land for landscape and port for the portrait. When the screen is vertically shown, the orientation is portrait and when the screen is horizontally shown the orientation is landscape.

10. UI Mode for Android Application

You can set some user interface modes for your Android application by using this resource. i.e. car, desk, television, and appliance.

11. Night Mode for Android Application

You can set or unset night mode for your Android application by using this resource i.e. night and notnight.

12. Screen Pixel Density for Android Application

dpi (dots per inches)  is used to set pixel density of screen for the Android application. You can set low, medium, high and extra-high density for the resource so when the screen density matches, the resource is automatically loaded. You can set these screen densities by using ldpi, mdpi, hdpi, xhdpi, tvdpi, and nodpi  for Android application.

  1. ldpi for low density (120dpi)
  2. mdpi for medium density (traditional HVGA screens of 160dpi)
  3. hdpi for high density (240dpi)
  4. xhdpi for extra high density (320dpi)
  5. nodpi for no specific density
  6. tvdpi for television density

13. Touchscreen Type for Android Application

You can set the touchscreen mode on or off by using notouch, finger resource in Android Application.

14. Keyboard Availability for Android Application

you can expose keys by using keysexposed, hide keyboard by using keyshidden and show soft keys by using keyssoft in Android application.

15. Primary Text Input Method for Android Application

In Android application, you can use nokeys, if you want no primary input method, qwerty, if you want to show complete qwerty keyboard as text input and 12key, if you want to show only 12 keys keypad i.e. the traditional old keypad.

16. Navigation Key Availability for Android Application

you can show or hide navigation keys for your Android application by using navexposed to show navigation keys and navhidden if you want to hide navigation keys.

 17. Primary Non‐touch Navigation Method for Android Application

You can set the primary navigation method for your Android application i.e. by using nonav for no navigation method, dpad for dial pad, trackball for trackball and wheel to use the wheel as primary navigation method.

18. Platform Version for Android Application

You can specify the platform version for your Android application. This platform version is the API level supported by the device. V1 for API level 1 (devices with Android version 1.0 or higher), V4 for API level 4 (devices with Android version 1.6 or higher) and so on.

Android Application Resource ID

Each resource must have a type in which it is grouped in, like string, drawable or layout.

Each resource must have a resource name, which is either the file name(without extension) or the value in XML android:name attribute  if the resource is a simple value (like string).

Accessing Android Application Resources

Once you provide a resource to your Android application, you can easily access it using its resource ID. These Android application resource IDs are automatically generated and placed in Resouce (R.java) file. Each of the resource ID is placed in the subclass of automatically generated R.java file. For example, R.drawable for all the drawable file.

If you have placed an icon.png in the drawable directory so to access the icon inside your code you must use R.drawable.icon which returns a static integer, this static integer is the resource ID of that specific drawable resource named as icon.png.

There are two ways to access a resource.

  1. In XML
  2. In Code

 

1. Accessing Android Application Resources in XML File

In XML file you can access any resource using the special syntax for XML which also corresponds to the resource ID. For example, if you have a string in strings.xml file named as heading with value Some Heading, so you can access it in XML layout file by using @string/heading where expected. In this example, the string is the resource type, and heading is the resource name.

If you want to add a new resource ID, which is not already present in the resource you can use + character after @ sign like @+button_text. This will create a new id with name, button_text.

Syntax to Reference a Resource in XML

<package_name> is the name of the package in which the resource is located. This is not required when you are referencing a resource from the same package
<resource_type> is the R subclass for the resource type. For example, drawable, layout or string.
<resource_name> is either the resource filename without extension or the android :name attribute value in the XML like for simple string resource.

2. Accessing Android Application Resources in Code

In your code, you can access any resource through R.java file. By using subclass i.e. the type of the resource in Resource R.java file and the name of the resource.

Syntax to Reference a Resource in Code

<package_name> is the name of the package in which the resource is located. This is not required when you are referencing a resource from the same package
<resource_type> is the R subclass for the resource type. For example, drawable, layout or string.
<resource_name> is either the resource filename without extension or the android :name attribute value in the XML like for simple string resource.

Use ImageView to Load Image in Code

If your image is placed in the drawable subdirectory, then the directory address will be
res/drawable/image.png
Access the resource using setImageResource() method in ImageView so first create an Image View and load the image resource in image View.

 Set Activity Title from String Resource in Code

If your string name is activity_name in strings.xml then you can access the resource in your code by using the resource ID and resource type.
R.string.activity_name
If you want to set the Android Activity title from a string in strings.xml file.

 

Set Text on TextView from String Resource

If your string name is activity_name in strings.xml then you can access the resource in your code by using the resource ID and resource type.
R.string.message

So you can access the message from strings.xml file in your code by

 

Learn more about resources.

Hope you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!
In the next tutorial, you will learn SQLite database in Android.
Don’t forget to give your feedback through comments.