Subscribe:

Ads 468x60px

dropdown

Social Icons

Android Resources


 Resources

Android supports that resources, like images and certain XML configuration files, can be keep separate from the source code.
These resources must be defined in the res directory in a special folder dependent on their purpose. You can also append additional qualifiers to the folder name to indicate that the related resources should be used for special configurations, e.g. you can specify that a resource is only valid for a certain screen size.
The following table give an overview of the supported resources and their standard folder prefix.

Table 1. Resources

Resource
Folder
Description
Simple Values
/res/values
Used to define strings, colors, dimensions, styles and static arrays of strings or integers. By convention each type is stored in a separate file, e.g. strings are defined in theres/values/strings.xml file.
Layouts
/res/values
XML file with layout description files used to define the user interface for activities and Fragments.
Styles and Themes
/res/values
Files which define the appearance of your Android application.
Animations
/res/animator
Define animations in XML for the property animation API which allows to animate arbitrary properties of objects over time.
Menus
/res/menu
Define the properties of entries for a menu.

The gen directory in an Android project contains generated values. R.java is a generated class which contains references to certain resources of the project.
If you create a new resource, the corresponding reference is automatically created in R.java via the Eclipse ADT tools. These references are static integer values and define IDs for the resources.
The Android system provides methods to access the corresponding resource via these IDs.

For example to access a String with the R.string.yourString ID, you would use thegetString(R.string.yourString)) method.

R.java is automatically created by the Eclipse development environment, manual changes are not necessary and will be overridden by the tooling.


Assets

While the res directory contains structured values which are known to the Android platform, the assets directory can be used to store any kind of data. You access this data via the AssetsManager which you can access the getAssets()method.
AssetsManager allows to read an assets as InputStream with the open() method.

// Get the AssetManager
    AssetManager manager = getAssets();

    // Read a Bitmap from Assets
    InputStream open = null;
    try {
      open = manager.open("logo.png");
      Bitmap bitmap = BitmapFactory.decodeStream(open);
      // Assign the bitmap to an ImageView in this layout
      ImageView view = (ImageView) findViewById(R.id.imageView1);
      view.setImageBitmap(bitmap);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (open != null) {
        try {
          open.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }


Defining IDs

Android allows that you define ID of user interface components dynamically in the layout files, via the @+id/your_idnotation.
To control your IDs you can also create a file called ids.xml in your /res/values folder and define all IDs in this file.

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

    <item name="button1" type="id"/>

</resources>

This allow you to use the ID directly in your 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"
    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>


0 comments:

Post a Comment