Hello ReadersJ!! Hope you have enjoyed past few tutorials. Let us start a new section of resources. Android applications are composed of two things and they are:
- Code instructions
- 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.
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 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 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 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 with 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.
Create a project and name it StringResourceExample app. Open the activity_main.xml file and code it as shown in the following listing:
Figure activity_main.xml file
Open the file saying strings.xml under res directory. Open the file and code it as shown in the following listing:
Figure strings.xml file
Graphical layout should be similar to the following snapshot:
Figure Graphical layout of application
Open the manifest file and cross check with the following listing:
Figure manifest file of app
Create a suitable emulator and run the application. Output should be similar to the following snapshot:
Figure Output of application
Congratulations crowdJ!! We are done. See you in the next section. Till then keep practicing and learning. Happy App Developing!!!