Website Builder

Resources Organizing & Accessing

Lesson 7a

There are many more items which you use to build a good Android application. Apart from coding for the application, you take care of various other resources like static content that your code uses, such as bitmaps, colors, layout definitions, user interface strings, animation instructions, and more. These resources are always maintained separately in various sub-directories under res/ directory of the project.


This lesson will explain you how you can organize your application resources, specify alternative resources and access them in your applications.


Organize Resources

You should place each type of resource in a specific subdirectory of your project's res/ directory. For example, here's the file hierarchy for a simple project:


    MyProject/

        src/

                MyActivity.java

        res/

                drawable/

                        icon.png

                layout/

                        activity_main.xml

                        info.xml

                values/

                        strings.xml


The res/ directory contains all the resources in various subdirectories. Here we have an image resource, two layout resources, and a string resource file.


Alternative Resources

Your application should provide alternative resources to support specific device configurations. For example, you should include alternative drawable resources ( ie.images ) for different screen resolution and alternative string resources for different languages. At runtime, Android detects the current device configuration and loads the appropriate resources for your application.


To specify configuration-specific alternatives for a set of resources, follow the following steps:


  • Create a new directory in res/ named in the form <resources_name>-<config_qualifier>. Here resources_name will be any of the resources mentioned in the above table, like layout, drawable etc. The qualifier will specify an individual configuration for which these resources are to be used. You can check official documentation for a complete list of qualifiers for different type of resources.


  • Save the respective alternative resources in this new directory. The resource files must be named exactly the same as the default resource files as shown in the below example, but these files will have content specific to the alternative. For example though image file name will be same but for high resolution screen, its resolution will be high.


            Visit the Library for ebook and video tutorial.


Accessing Resources

During your application development you will need to access defined resources either in your code, or in your layout XML files. Following section explains how to access your resources in both the scenarios:


ACCESSING RESOURCES IN CODE

When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.



Now if you will go through previous lesson once again where we explained Hello World Android Example, you will have better understanding on all the concepts explained in this lesson.

Activities

Lesson 7b

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched.


If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main( ) function. Very similar way, Android system initiates its program with in an Activity starting with a call on onCreate( ) callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity lifecycle diagram: (image courtesy : android.com )


Visit the Library for indepth ebook and video tutorial.

Ask your questions and comments below

Your questions and comments should be based on the lesson.