Android Basic Training Course: Working with Resources

 
    Resources are static bits is held outside the Java source code. Have you ever seen one kind of resources - layouts - often appear in the examples in this course. There are many different types of resources, such as images and strings, you can use them in your Android applications.

Resources types

    These resources are stored as file format in the "res" folder in your Android project. Except exception with raw resources ("res/raw"), all kinds of resources have been parsing help you, either by the system's packaging or by Android system Android on the device or VM . So when you arrange for an activity interface through a layout resource ("res/layout"), you do not need parse the XML layouts because Android already do this for you.
    In addition to the layouts resource, there are many other resources available to you, including the following types:
Directory
Resource Type
anim/
XML files that define property animations. They are saved in res/anim/ folder and accessed from the R.anim class.
color/
XML files that define a state list of colors. They are saved in res/color/ and accessed from the R.color class.
drawable/
Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps, state lists, shapes, animation drawable. They are saved in res/drawable/ and accessed from the R.drawable class.
layout/
XML files that define a user interface layout. They are saved in res/layout/ and accessed from the R.layout class.
menu/
XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. They are saved in res/menu/ and accessed from the R.menu class.
raw/
Arbitrary files to save in their raw form. You need to callResources.openRawResource() with the resource ID, which isR.raw.filename to open such raw files.
values/
XML files that contain simple values, such as strings, integers, and colors. For example, here are some filename conventions for resources you can create in this directory:
  • arrays.xml for 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 class.
  • colors.xml for color values, and accessed from theR.color class.
  • dimens.xml for dimension values, and accessed from the R.dimen class.
  • strings.xml for string values, and accessed from theR.string class.
  • styles.xml for styles, and accessed from the R.style class.
xml/
Arbitrary XML files that can be read at runtime by calling Resources.getXML(). You can save various configuration files here which will be used at run time.

Now, I would like to introduce some important resources.

Strings Resource

    This is most popular resource type in Android. All static strings will be saved in res/strings.xml file. This file is auto-generated after we creating a new Android Studio project. Each string declared by <string> tag. For example:
strings.xml
<resources>
    <string name="app_name">Android Resources</string>
    <string name="demo">DEMO String</string>
</resources>
    After defining it like above, you can use them both xml layouts and programmatically code. In xml, use it with  @string/[string_name]:
<TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/demo" />
    In Java code, in the current Context (usually is your Activity), with getResources().getString(), you can use these strings resource:
TextView textView = (TextView)findViewById(R.id.text);
textView.setText(this.getResources().getString(R.string.demo)); // set TextView content

Images Resource

    Android supports the PNG image format, JPEG and PNG format is most preferred. The images can be used wherever you require a Drawable object, such as an image or a background of an ImageView.
    Using images resource is putting your photos into the "res/drawable" folder and then refer to them as a resource. From within the layout files, the images are referenced through @drawable/[image_name] (e.x: with @drawable/[image_name], the name of the resource will be @drawable/foo). In Java, when you need a photo ID of a resource, just use R.drawable.[image_name](e.x: R.drawable.foo).
    For example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin">

    <ImageView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:src="@drawable/green_dot" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_call"
        android:text="@string/call" />
</LinearLayout>
    Output:
    Like strings resource, you can use these drawables in Java code, this code is equivalent with xml defining above:
imageView.setImageResource(R.drawable.green_dot); // set Image for ImageView
button.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_call, 0, 0, 0); // set left drawable for Button

Colors resource


    The colors in Android valuable in HEX format with an additional option to specify an alpha channel. You can select the type of a character value 16 or value 16 type 2 characters, there are 4 styles as below:
  • #RGB
  • #ARGB
  • #RRGGBB
  • #AARRGGBB
    All colors resource available in res/values/colors.xml file:
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>
    You can use theme like strings or images resources.

Styles/Themes resource

 
    A special resource type in Android, which styling objects, define the look and format for UI elements or further, declaring theme for your app. For details about this, you can read at next chapter: Styles and Themes in Android.

Conclusions

 
    In this tutorial, we looked at the basics of app resources on Android. But as you have seen, there is much more to explore. So, you can go to the official page of Android SDK to deep understanding about Resources. Moreover, you can read this page to know details of all resources types.

Share


Previous post
« Prev Post
Next post
Next Post »