Using Resources
Objective #1: Externalize Resources.
- It is important to externalize resources such as images, layouts, and strings so it is easy to use them with different hardware devices or with different languages. Do not hard-wire these kinds of resources into your programming code.
- The resources should be organized in subfolders in the project res folder. From there, they can be accessed using their resource ID's that are found in the project's R class.
- See this link - http://developer.android.com/guide/topics/resources/providing-resources.html - for the names of the subfolders where you should store the various resources.
- See this link - http://developer.android.com/guide/topics/resources/available-resources.html - for information about the resource types that can be used. You should only use lowercase letters for all folder and file names. See the same link for information about aliasing resources with multiple name identifiers.
- Resource names for files are the lowercase spelling of the filename without file extensions. Resource names for some resources such as strings are the name attribute found in the XML.
In your Java code, a resource can be specified by typing something like R.string.hello_message where hello_message is the resource name of a string resource type. For example,
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello_message);
sets the text on a TextView object with the resource id of msg to the text that is found in the string resource named hello_message that is stored in the string.xml file.
In XML, that resource can be specified by typing android:text="@string/hello_message"
Objective #2: Use Drawable resources.
- A drawable resource is basically a bitmap graphic that can be drawn on the screen such as a graphic file or an XML file that defines a graphic.
- The graphic file types png, jpg, and gif can be used however png is preferred and gif is discouraged.
- To include a graphic in your app's UI:
- Place the graphic file in the res/drawable folder. The resource ID for the graphic will be its filename not including the file extension. For example, if the file is named image.png then the resource ID is image. It is recommended that you avoid using symbols or blank spaces in the filename. Do not use files with names like angry bird.png though you can use underscores as in angry_birds.png.
- Add the following statement to the onCreate method in order to include a resource graphic named image.png, image.jpg, or image.gif. Note that the graphic file's extension is not included as part of its resource ID.
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageResource(R.drawable.image);
- An ImageView XML reference must be added to an XML layout file in order to include a graphic in the app's UI:
<ImageView android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
The name of the graphic file that is included in the res/drawable folder would be named image.png, image.jpg, or image.gif.