May 21, 2021 Android
There's a lot to build for a great Android app. I n addition to application encoding, you need to focus on a variety of resources, such as the various static content you use, such as bitmaps, colors, layout definitions, user interface strings, animations, and so on. These resources are typically placed in the project's res/under-separate subdirector.
This tutorial will learn how to organize application resources, specify alternative resources, and access them in your application.
You need to place each resource under a specific subdirector in the project res/directory. For example, this is the file level of a simple project:
MyProject/ src/ MyActivity.java res/ drawable/ icon.png layout/ activity_main.xml info.xml values/ strings.xml
The res/directory contains all the resources in various subdirectdirectles. H ere is a picture resource, two layout resources, and a string resource file. The following table details the resources supported in the project res/directory.
Directory | The resource type |
---|---|
anim/ | The XML file that defines the animation properties. They are saved under res/anim/folders and accessed through the R.anim class |
color/ | The XML file that defines the list of color states. They are saved under the res/color/folder and accessed through the R.color class |
drawable/ | Picture files, such as .png, .jpg, .gif, or XML files, are compiled into bitmaps, status lists, shapes, animated pictures. They are saved under res/drawable/folder and accessed through the R.drawable class |
layout/ | XML files that define the layout of the user interface. They are saved under res/layout/folders and accessed through the R.layout class |
menu/ | XML files that define application menus, such as option menus, context menus, sub-menus, etc. They are saved under res/menu/folder and accessed through the R.menu class |
raw/ | Any files are saved in their original form. The raw file needs .raw be opened by calling Resource.openRawResource() based on a resource ID called R.filename |
values/ | An XML file that contains simple values such as strings, integers, colors, and so on. H ere are some resource naming conventions under folders. A rrays.xml represent array resources, accessed through the R.array class.xml.xml.xml the system represents the color resource, accessed through the R.color class, dimens.xml represents the dimension value, accessed through the R.dimen class, strings.xml represents the string resource, accessed through the R.string class, and styles .xml represents the style resource, through R.st yle class access |
xml/ | You can read any XML file at runtime by calling Resources.getXML(). You can save the various profiles that you use at runtime here |
Your application needs to provide alternative resource support for a specific device configuration. F or example, you need to provide alternative picture resources for different screen resolutions and alternative string resources for different languages. At runtime, Android detects the current device configuration and loads the appropriate resources for the application.
To identify a series of alternative resources for a specific configuration, follow these steps:
Below is an example that specifies the default screen for the picture and a high-resolution alternative picture.
MyProject/ src/ main/ java/ MyActivity.java res/ drawable/ icon.png background.png drawable-hdpi/ icon.png background.png layout/ activity_main.xml info.xml values/ strings.xml
Here's another example, specifying the layout of the default language and an alternative layout for the Arabic language.
MyProject/ src/ main/ java/ MyActivity.java res/ drawable/ icon.png background.png drawable-hdpi/ icon.png background.png layout/ activity_main.xml info.xml layout-ar/ main.xml values/ strings.xml
In application development, you need access to well-defined resources, whether through code or XML files. The following sections describe how to access resources in both scenarios.
When the Android application is compiled, an R class is generated that contains the ID of all the resources in the res/directory. You can use the R class to access resources through sub-classes and resource names, or directly with resource IDs.
Visit the res/drawable/myimage .png and set it up on ImageView, and you'll use the following code:
ImageView imageView = (ImageView) findViewById(R.id.myimageview); imageView.setImageResource(R.drawable.myimage);
The first line of code here uses R.id.myimageview to get ImageView defined as myimageview in the layout file. The second line uses R.drawable.myimage to get a picture called myimage under the drawable subdirecter of res/.
Consider the next example, where the res/values/strings .xml as follows:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello, World!</string> </resources>
Now you can use the resource ID to set the text on a TextView object with an ID of msg, as follows:
TextView msgTextView = (TextView) findViewById(R.id.msg); msgTextView.setText(R.string.hello);
Consider the layout res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a TextView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a Button" /> </LinearLayout>
This application code will load this layout for the activity, as follows in the onCreate() method:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); }
Consider the following XML resource file res/values/strings .xml, which contains a color resource and a string resource -
<?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/opaque_red" android:text="@string/hello" />
You can now use these resources in the following layout files to set the text color and text content:
<?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/opaque_red" android:text="@string/hello" />
Now, if you go back to the previous chapter, "Hello World! " Examples, I can be sure that you have a better understanding of all the concepts in this section. So, I strongly recommend going back to the previous example and looking at the basic usage of different resources.