Resources in Android

Posted By on March 21, 2016


Download PDF
The Android manifest
Android views - UI Widgets

Android allows you to create static resources like images and XML configuration files. This allows you to keep these resources separate from the source code of your Android application.

Resource files must be placed in the /res directory of your application in a predefined sub-folder. The specific sub-folder depends on type of resource which is stored. You can also append additional qualifiers to the folder name to indicate that the related resources should be used for special configurations. This are called resource qualifiers. For example, you can specify that layout file is only valid for a certain screen size.

The following table gives an overview of the supported resources and their standard folder prefixes.

Table. Resources

Resource Folder Description
Drawables /res/drawables Images (e.g., png or jpeg files) or XML files which describe a Drawable object. Since Android 5.0 you can also use vector drawables which scale automatically with the density of the Android device.
Simple Values /res/values Used to define strings, colors, dimensions, styles and static arrays of strings or integers via XML files. By convention each type is stored in a separate file, e.g., strings are defined in theres/values/strings.xml file.
Layouts /res/layout XML files with layout descriptions are used to define the user interface for Activitiesand fragments.
Styles and Themes /res/values Files which define the appearance of your Android application.
Animations /res/animator Defines animations in XML for the animation API which allows to animate arbitrary properties of objects over time.
Raw data /res/raw Arbitrary files saved in their raw form. You access them via anInputStream object.
Menus /res/menu Defines the actions which can be used in the toolbar of the application.

2. Example: Defining strings, string arrays, colors and dimensions

The following listing is an example for file called values.xml in the /res/values which defines a few String constants, a String array, a color and a dimension.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Test</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="operationsystems">
        <item>Ubuntu</item>
        <item>Android</item>
        <item>Microsoft Windows</item>
    </string-array>

    <color name="red">#ffff0000</color>

    <dimen name="mymargin">10dp</dimen>

</resources>

3. Resource files and R.java

Every relevant resource in the res folder, gets an ID assigned by the Android build system. Android generates gen a R.java file which ccontains these generated values. These references are static integer values.

If you add a new resource file, the corresponding reference is automatically created in a R.java file. Manual changes in the R.java file are not necessary and will be overwritten by the tooling. The Android system provides methods to access the corresponding resource files via these IDs.

For example, to access a String with the R.string.yourString ID in your source code, you would use the getString(R.string.yourString) method defined on the Context class.

4. Layout files

Android activities define their user interface with views (widgets) and fragments. This user interface can be defined via XML layout resource files in the /res/layout folder or via Java code. You can also mix both approaches.

Defining layouts via XML layout files is the preferred way. This separates the programming logic from the layout definition. It also allows the definition of different layouts for different devices.

A layout resource file is referred to as layout. A layout specifies the ViewGroups, Views, their relationship and their attributes via an XML representation.

The following code is an example for a simple layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/mytext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

A layout is assigned to an activity via the setContentView() method calls, as demonstrated in the following example code.

package com.vogella.android.first;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

5. Performance considerations with layouts

Calculating the layout and drawing the views is a resource intensive operation. You should use the simplest layout possible to achieve good performance. For example, you should avoid nesting layout managers too deeply or avoid using complex layout managers in case a simple layout manager is sufficient.

6. Good practices for resources IDs

If a view needs to be accessed via Java or XML code, you have to give the view a unique ID via theandroid:id attribute. To assign a new ID to a view use the android:id attribute of the corresponding element in the layout file.

The Android SDK uses the camelCase notation for most of its IDs, e.g., buttonRefresh. It is good practice to follow this approach.

The following shows an example in which a button gets the button1 ID assigned via theandroid:id="@+id/button1" parameter. By conversion this statement creates a new ID if necessary in the R.java file and assigns the defined ID to the corresponding view.

<Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Show Preferences" >
</Button>

It is good practice to define IDs in one central configuration file. This is typically called ids.xml and placed in the /res/values folder. This allows you to use the predefined ID in your layout file. If you want to define the id in a separate file, you first need to remove the @+id entries in your layout files, otherwise you get an error messages that these files have already been created. The following listing shows an example for such a file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="button1" type="id"/>
</resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="27dp"
        android:text="Button" />

</RelativeLayout>

Note

While the above is good practice for real projects, the tutorials of this book avoid the usage of a separate id file as this setup is time consuming.

7. System resources

Android also provides resources. These are called system resources. System resources are distinguished from local resources by the android namespace prefix. For example,android.R.string.cancel defines the platform string for a cancel operation.

The Android manifest
Android views - UI Widgets

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby

Website: http://world4engineers.com