Android Resources

0
825

There are numerous softwares which you can use for the Android application development. With the help of these software, you can write code for your application, you can handle various resources including static content. The static content of an application include bitmaps, background and text color, layout definitions, and many more. All of this static content are generally located in different sub-directories under the root directory of the project i.e., res/.

Also read: Android Hello World Application

With the help of this tutorial, you will get to know how to access and organize application resources in an Android application. Other than that, you will also get to know how to specify alternative application resources.

Organizing Application Resources Using Android Studio
MyProject/
   app/
      manifest/
         AndroidManifest.xml
   java/
      MyActivity.java  
      res/
         drawable/  
            icon.png  
         layout/  
            activity_main.xml
            info.xml
         values/  
            strings.xml

Now, take a look at the table provided below. You have application resources and the directory they belong to. You can also take a look at the descriptions of these application resources.

S. No. Directory & Resource Type Description
1 anim/ * XML files defining property animation
* Resides in res/anim/ directory
* You can access this resource with the help of R.anim class
2 color/ * XML files defining color state list
* Resides in res/color/ directory
* To access this resource, you need to access using R.color class
3 drawable/ * Consists of various image formats like .png, .jpg, .gif
* Compiled XML files of bitmaps, state lists, shapes and animation
* Resides in res/drawable/ directory
* To access this resource, use R.drawable class
4 layout/ * XML files defining user interface layouts for your application
* Resides in res/layout/ directory
* Access it with the help of R.layout class
5 menu/ * XML files defining various kinds of application menu
* Resides in res/menu/ directory
* It can be accessed through R.menu class
6 raw/ * Arbitrary files being stored in their raw or unprocessed form
* Use R.raw.filename to call Resources.openRawResource() method
* Resource ID – R.raw.filename used to open raw files
7 values/ * XML files defining strings, integers, and colors

Below are a few filename conventions for resources
* arrays.xml – for resource arrays
– can be accessed via R.array class

* integer.xml – for resource integers
– can be accessed via R.integer class

* bools.xml – for resource boolean
– can be accessed via R.bool class

* colors.xml – for resource color values
– can be accessed via R.color class

* dimens.xml – for dimensional values
– can be accessed via R.dimen class

* strings.xml – for resource string values
– can be accessed via R.string class

* styles.xml – for resource styles
– can be accessed via R.style class

8 xml/ * XML files which can be read at the runtime
* To read these XML files, call Resources.getXML() method
* Numerous configuration files can be stored in this directory

 

Alternative Application Resources

Your Android application will also be carrying alternative application resources. The alternative resources are used to support various yet specific device configurations. For an instance, alternative drawable resources i.e., images are used to support different screen resolutions. Other than that, the alternative resources like alternative strings resources are used to support your application in different languages. These alternative resources work at the runtime of the application. At the application’s runtime, the Android platform detects the configuration for the current device. It also helps in loading the suitable resources as per your application’s requirement.
How to specify configuration based alternatives for various resources?
Follow the given steps to define the configuration based alternatives for several resources.

Step 1: Go to res/ directory. Here, create a new folder under the name which is in the form of <resources_name>-<config_qualifier>. You have to replace <resources_name> with a resource, which you can pick from the above form. So, you can replace it with color, values, drawable, layout, etc. After this, replace <config_qualifier> with the specific configurations for the various resources.  You can get the list of qualifiers from the official documentation of Android resources. Refer to table 2 on the webpage.

Step 2: Store the appropriate alternative resources in this newly created directory. Make sure that the resource files’ name should tally with the default resource files. Though these newly created files will be having content specific to the alternative resources. You can take a look at the given example to understand this more clearly. For example, even if the names of the image files are same but they do have high resolution screen size.

Take a look at this example where image files for default screen are specified along with the alternative image files for high screen resolution.

MyProject/
   app/
      manifest/
         AndroidManifest.xml
   java/
      MyActivity.java   
      res/
         drawable/  
            icon.png
            background.png
         drawable-hdpi/  
            icon.png
            background.png  
         layout/  
            activity_main.xml
            info.xml
         values/  
            strings.xml

In another example, the layout has been specified for default languages along with the alternative layouts for the Arabic language.

MyProject/
   app/
      manifest/
         AndroidManifest.xml
   java/
      MyActivity.java   
      res/
         drawable/  
            icon.png
            background.png
         drawable-hdpi/  
            icon.png
            background.png  
         layout/  
            activity_main.xml
            info.xml
         layout-ar/
            main.xml
         values/  
            strings.xml

How to access application resources?
There are times when you will be required to access application resources in your application. This can be done either with the help of incorporating it into the code or in the layout XML files. Below, you will get to know how to integrate resources while application development.

How to access resources using programming codes?
With the compilation of Android application, an R class comes into existence. This R class consists of all the resource IDs for the respective application resources stored in res/ directory. To access any resource, you need to access it with the help of R class. It can be done using the subdirectory, or the resource name, or the resource ID.

To understand this more clearly, take a look at the given examples.

Example 1
Here, we will be accessing a drawable resource which is res/drawable/myimage.png. We will also be setting ImageView in the following code.

ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);

In the first line of the code: We are defining ImageView with myimageview id in the layout file. For this, we have used R.id.myimageview.

In the second line of the code: We are setting image resource as myimage from the subdirectory of res/ directory. For this, we have used R.drawable.myimage.

Example 2
Here, we are defining Hello World! string in our programming code.

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
   <string  name=”hello”>Hello, World!</string>
</resources>

It is being defined in res/values/strings.xml file. Now, follow the given code to set the text on TextView object with the help of resource ID.

TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);

Example 3
Here, we are defining the layout in the res/layout/activity_main.xml file.

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
   android:layout_width=”fill_parent”
   android:layout_height=”fill_parent”
   android:orientation=”vertical” >

   <TextView android:id=”@+id/text”
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Hello, I am a TextView” />

   <Button android:id=”@+id/button”
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Hello, I am a Button” />
</LinearLayout>

Now, using onCreate() method, you will be able to load the layout for an Activity.

public void onCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
}

How to access application resource through XML files?
Below is a section of application code where res/values/strings.xml file contains color resource and a string resource.

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
   <color name=”opaque_red”>#f00</color>
   <string name=”hello”>Hello!</string>
</resources>

Now, you will be able to use the above defined application resources with the help of layout XML files. Follow the given code to set the text color and the text string in your application.

<?xml version=”1.0″ encoding=”utf-8″?>
<EditText xmlns:android=”http://schemas.android.com/apk/res/android”
   android:layout_width=”fill_parent”
   android:layout_height=”fill_parent”
   android:textColor=”@color/opaque_red”
   android:text=”@string/hello” />

This chapter will surely help you to understand previous modules, especially Hello World! example. It is recommended to go through the previous modules again to have better clarity of the application code.