In this chapter we will learn about Android resources. Android applications are composed of two things and they are:
- Android Code instructions
- Android Data / resources
Functionality is in the instruction coded which determines the way our application should behave. Other section of importance is resources i.e. text, images, icons, audio files, videos, etc.
5.1 Introduction to Android resources
Android resource files are stored separately from the rest of java files of application. Generally resources are stored as xml files. Graphics and raw data can be stored as resources.
All resources must be stored in the /res directory of project. So directory hierarchy is strictly maintained. Each resource type corresponds to a particular sub-directory. All the graphics files are stored under /res/drawable folder of directory. In fact it is a good practice to keep non code resources external to our code. Thus we can say android supports externalization of resources. It can range from simple values such as strings, colors to complex things like images, etc.
By storing these resources externally we ease their management, maintenance and updates. Android is intelligent enough to select correct resources without interruption of user explicitly. Thus customization becomes much easier.
Resource sub directory
Simple data like strings, color values, etc
Table Default resource directories of android
ADT plug-in automatically detects any new resource file added to the project. These resources are compiled. This generates the R.java file which enables us to access resources programmatically.
5.2 Resolving Android Resources
There are few applications which work perfectly in every environment. But some special case handling is required. We can create alternative resources for android projects. Android resources can be organized into different resource types. Resources can be categorized according to screen characteristics, language and region, input methods, device configurations, etc. So we can say we have two resource hierarchies as shown below:
- Default resources are the resources which are stored at top of hierarchy. These resources are used regardless of their device configurations.
- Alternative resources are the specialized versions of these default resources. These are the resources designed while keeping in mind for the purpose of a specific configuration.
Figure - Android Resource hierarchy
There are two specific reasons for opting alternative resources and they are as follows:
The above listed features facilitate run of applications universally in different device configurations. Android platform has a superb mechanism of loading appropriate resources at runtime. When we create alternative resources few things are to be kept in mind.
- Android platform is intelligent enough to load the most perfect available resource. In case there is no alternative resource then default resource is used. Thus alternative resources should be added after intelligent and suitable assessment of requirements.
- Alternative resources are to be named exactly as default resource.
- Alternative resource put burden over size of application package. Performance can be affected by their addition to application package. It is best to manipulate default resources so that it can add flexibility to application.
5.3 Accessing Android Resources
Android system provides system resources. All of these resources can be used directly from our application code. They can be referenced from within other resources.
We can access the resources in code by the static R class. This class is generated based on our external resources. They are created when the project is compiled. R class contains the sub classes for each resource type. Each of the subclass exhibits its associated resources as variables. These variables have a variable name which matches the resource identifiers. By default, a new project consists of R.string and R.drawable subclasses. The values of these variables are not the instance of resource itself. The value is an integer which represents particular resource’s location in the resource table.
When an instance of resource is required we need to use helper methods which in turn extract them from resource table. The resource table is represented as an instance of Resources class within the application. These helper methods look ups the current resource table. Hence these helper methods cannot be static.
getResources() method is used to access applications’ Resources instance i.e. we would have the following code snippet:
Resources classes include getters for each available resource type which usually works by passing resource ID.
We can also refer resources within other resources. We can use resource references as attribute values in other xml resources. In case of layouts and styles it is the best option. We can create themes; localize strings and image assets as per various sources. It is also a suitable method to support different images and spacing for a layout to ensure that it is optimized for different screen sizes and resolutions. The @ notation should be used to refer one resource from another.
Android avails lot of native system resources which we can use in our applications directly. When we use native resources of android they are made available to us from classes of android.R and not from any specific class of R class.
5.4 Android Resource Example
Create a project and name it StringResourceExample app. Open the activity_main.xml file and code it as shown in the following listing:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_below="@+id/textView2" android:layout_marginTop="40dp" android:text="@string/intro" android:textColor="@android:color/darker_gray" android:textSize="30dip" android:textStyle="italic" /> </RelativeLayout>
Figure - activity_main.xml file od Android resouce example
Open the file saying strings.xml under res directory. Open the file and code it as shown in the following listing:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">StringResourceExample</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="intro">Hello Readers :-) This is just an demo text to demonstrate you the string resource file of application and its usage</string> </resources>
Figure - strings.xml file of Android resource example
Graphical layout should be similar to the following snapshot:
Figure - Graphical layout of Android resource application
Open the manifest file and cross check with the following listing:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.tution.Resource" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.android.tution.Resource.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
Figure - manifest file of Android resource example app
Create a suitable emulator and run the application. Output should be similar to the following snapshot:
Android resource example app output on emulator
Congratulations !!! We are done. See you in the next section. Till then keep practicing and learning. Happy App Developing!!!