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 using a resource ID.
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.
- Place the graphic file in the res/drawable folder. The resource ID for the graphic will be its filename not including the file extension.