Android Resources Organizing and Acccessing

Android Resources Organizing and Accessing

Android Resources it is title of tutorial so we will explain about it. So lets start, As You know there are many things which you use to develop a decent Android application. Aside from coding for the application, you deal with different resources like static content that your code uses, for example, bitmaps, hues, format definitions,layout, UI strings, animation instructions and much more. These resources are constantly kept up independently in different sub-directories under res/directory of the undertaking/project.

android resources

This tutorial exercise will clarify you how you can sort out your application resources, determine alternative resources and access them in your applications.

  • Organize Resources in Eclipse

You ought to put every kind of resources in a particular subdirectory of your task’s res/directory. For instance, here’s the file order for a straightforward project:

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

 

The res/directory contains every one of the resources in different sub directories. Here we have a picture resource, two layout resources, and a string resource document. Taking after table gives an insight about the resource directories supported inside project res/directory.

  • Organize Resources in Android Studio
MyProject/
    src/
  main/
  java/
     MyActivity.java  
    res/
        drawable/  
            icon.png  
        layout/  
            activity_main.xml
            info.xml
        values/  
            strings.xml

 

 Directory and Resource Type

  1. anim/    XML files that define property animations. they are stored in res/anim/ folder and accessed from the R.anim magnificence.
  2. color/    XML files that define a state listing of colors. they are stored in res/color/ and accessible from the R.color elegance.
  3. drawable/    image documents like .png, .jpg, .gif or XML documents which can be compiled into bitmaps, state lists, shapes, animation drawable. they may be saved in res/drawable/ and accessed from the R.drawable class.
  4. layout/    XML documents/files that outline a user interface layout. they are stored in res/layout/ and accessed from the R.layout magnificence.
  5. menu/    XML files that outline application menus, such as an alternatives menu, context menu, or submenu. they’re stored in res/menu/ and accessed from the R.menu class.
  6. raw/    arbitrary files to save in their raw shape. you need to name Resources.openRawResource() with the resource ID, which is R.raw.filename to open such raw documents/files.
  7. values/    XML files that include simple values, inclusive of strings, integers, and colorations. for example, here are a few filename conventions for sources you may create in this directory −
  • arrays.xml for useful resource arrays and accessed from the R.array class.
  • integers.xml for resource integers and accessed from the R.integer class.
  • bools.xml for resource boolean and accessed from the R.bool magnificence.
  • colors.xml for color values and accessed from the R.color class.
  • dimens.xml for dimensions values and accessed from the R.dimen class.
  • strings.xml for string values and accessed from the R.string class.
  • styles.xml for styles and accessed from the R.styleclass.

8. xml/    Arbitrary XML files that may be read at runtime through calling Resources.getXML(). you               could save diverse configuration files right here so as to be used at run time.

Alternative Android Resources

Your application should give alternate android resources to support particular device arrangements. For instance, you ought to incorporate option drawable assets ( i.e.images ) for various screen resolution and option string resources for various languages. At runtime, Android identifies the present device arrangement/configuration and burdens the appropriate resource for your app.

To indicate configuration particular choices for an arrangement of resources, just follow the steps which are given below

  • Make another directory in res/named in the structure <resources_name>-<config_qualifier>. Here resources_name will be any of the resources specified in the above table, similar to ,format, drawable and so on. The qualifier will indicate an individual arrangement for which these resources are to be utilized. You can check official documentation for a complete rundown of qualifiers for various kind of resources.
  • Save the individual alternate resource in this new index/directory. The resource files must be named precisely the same as the default resource files as appeared in the underneath case, yet these files will have content particular to the alternate. For instance, however, picture file name will be same yet for the high-resolution screen, its resolution will be high.

The following is example which indicates pictures for a default screen and alternative pictures for high determination/resolution screen.

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

 

The following is another example which determines layout for a default language and option layout for Arabic language.

MyProject/
   src/
  main/
  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

 

Android Accessing Resources

During your application development you’ll have to be compelled to access outlined resources either in your code, or in your layout XML files. Following section explains the way to access your resources in each the scenarios –

  • Android Accessing Resources in Code

When your android application is compiled, a R category gets generated, that contains resource IDs for all the resources offered in your res/ directory. you’ll use R category to access that resource victimization sub-directory and resource name or directly resource ID.

Android Accessing Resources Example

To access res/drawable/myimage.png and set an ImageView you should use code which given below:

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

 

Above the first line code make used of R.id.myimageview to get ImageView defined with id myimageview in a Layout file. The Another an last line  of code makeused ofR.drawable.myimage to get an image with name myimage available in drawable sub-directory under /res.

Android Resources Example

Suupose next example where res/values/strings.xml has following code:

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

Now you can adjust the Text on a TextView object with ID msg by using  resource ID as follows

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

 

Android Resources Example

Suppose a layout res/layout/activity_main.xml with the following Code:

<?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>

 

This application code will load this layout for an Activity, in the onCreate() method as follows

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

 

  • Android Accessing Resources in XML

Suppose the following given resource XML res/values/strings.xml file that includes a 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 can use these resources in the following given layout file to set the text color and text string.

<?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" />

 

Now if you read previous Chapter once again where we have explained Hello World! practical example, and I hope  you will  understand better on all the concepts explained in this tutorial. So I highly recommend you to check previous Tutorial for working example and check how we have used various resources at very basic level.