Android Programming: Setting & Access Android Resources



Click to watch


Setting & Access Android Resources






Setting & Access Android Resources :



Setting & Access Android Resources


There are many things you need to use in order to create a good application. In addition to writing the code for the application, you need to pay attention to the resource like as static content that the code use. Such as bipmaps, colors, layout, strings, ... The resource is contained in various subfolders and belonging src/ folder.

In this article will introduce you how to set up these resources, determine the resources and how to access them in your application.

1. Setting Resource

You should put each type of resource in each different sub-folders and within parent res/ folder.

  src/  
        MyActivity.java  
    res/
        drawable/  
            icon.png  
        layout/  
            activity_main.xml
            info.xml
        values/  
            strings.xml 


res/ folder contain all subfolders. In the example above, we have 1 image resource, 2 layout resource, and 1 string resource.


Here are the details of resources directory are supported inside the res/ folder .

anim/: Contain the XML files that define the effect. They is accessed from R.anim class.

color/: Contain the XML file that define in color. They are accessible from R.color class.

drawable/: Contain the image files such as .png, .jpg, .gif or xml files have been compiled into bitmaps. They are accessible from R.drawable class.

menu/: Contain the XML files defining the application's menu, probably Options Menu, Context Menu, Sub Menu. They are accessible from R.menu class.

raw/: Contain any files, arbitrarily. Need to call Resources.openRawResource(). With the resource ID, it is R.raw.filename to open one raw files.

values/: Contain the XML files to store simple values such as strings, integers, colors, ...

       + arrays.xml: Used to define the array of data, and is accessible from R.array class.

       + bools.xml: Used to define the data type bool and is accessed from R.bool class.

       + colors.xml: Used to define the color values and is accessed from R.color class.

       + dimens.xml: Used to define the value of size, dimension and is accessed from R.dimen class.

       + strings.xml: Used to define the string and called from R.string class.

       + styles.xml: Used to define the styles and called from R.style class.


2. The Alternative Resources

Your application should support alternative resources to be able to better support on various devices.

For example: You should have different alternative drawable resources such as images to support devices with different screen resolution.

Or different alternative strings resources to support multiple languages.

At the time program running Android will detect the settings for configuration of devices and loads the appropriate resources.

The steps to determine the structures of alternative resources:

Create a new folder in the folder res/ in the form -. can be the name of any subdirectories as mentioned above (such as drawable/, color/, layout/, etc). will specify a separate configuration that the resources will be used.

Save the alternative resources in the newly created folder. The resources file must be named the same as the default resources file.

For example: Below are examples of default image resource for the device has low-resolution screen, and the alternative image resources for devices with high resolution screens.



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


3. Access to The Resources

While developing application, you needs to access resources files or in your code, or in the XML file. Here are instructions on how to export the resources file:


To be continued...
Share on Google Plus

About Unknown

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment