The best Android Resources (Resources) management Tutorial In 2024, In this tutorial you can learn Organizational resources in eclipse,Alternative Resources,Access to resources,
There are many things used to build an excellent Android app. In addition to coding the application, you need to focus on a variety of resources, such as you use a variety of static content, such as bitmaps, colors, layout definition, user interface strings, animation and so on. These resources are generally placed in the project's res / standalone subdirectory.
This section of the tutorial will learn how to organize your application resources, specify alternative resources, and access them in your application.
You will need to be placed in each resource under specific subdirectory of your project res / directory. For example, this is a simple file-level project:
MyProject/ src/ MyActivity.java res/ drawable/ icon.png layout/ activity_main.xml info.xml values/ strings.xml
res / directory contains all of the resources in various subdirectories. Here's a picture resource, two layout resources and a string resource file. The following table gives a detailed in the project res / directory support resources.
table of Contents | Resource Type |
---|---|
anim / | XML file that defines the animation property. They are saved in res / anim / folder, by type of access R.anim |
color / | XML file that defines the color status list. They are saved in res / color / folder, by type of access R.color |
drawable / | Image files, such as .png, .jpg, .gif, or XML file, is compiled as a bitmap, state list, shapes, animated images. They are saved in res / drawable / folder, by type of access R.drawable |
layout / | Custom UI XML file layout. They are saved in res / layout / folder, by type of access R.layout |
menu / | Custom application menu XML files, such as the Options menu, context menus, sub-menus. They are saved in res / menu / folder, by type of access R.menu |
raw / | Any files are saved in their original form. We need to R.raw.filename named resource ID, to open the raw file by calling Resource.openRawResource () |
values / | XML files contain simple values (such as strings, integers, color, etc.). Here are some resources under the folder naming conventions. arrays.xml on behalf of an array of resources, through R.array type of access; integers.xml represents an integer of resources accessed through R.integer class; bools.xml resources on behalf of Boolean values, type of access by R.bool; colors.xml resources on behalf of color, accessed through R.color class; dimens.xml representatives dimension values, through R.dimen type of access; strings.xml represents a string resource, through R.string type of access; styles.xml representatives style resource, by type of access R.style |
xml / | By calling Resources.getXML () to read arbitrary XML files at runtime. Various configuration files can be saved runtime here |
Your application needs to provide resources to support an alternative configuration for a specific device. For example, you need to provide an alternative picture of resources for different screen resolutions, providing an alternative string resources for different languages. At runtime, Android detects the current device configuration, and load the appropriate resources for an application.
To identify a set of alternative resources for specific configuration, following the steps of:
Here is an example, to specify the default picture of the screen and high-resolution image substitution.
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 is another example, to specify the default layout language and alternative layouts 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, we need access to defined resources, either through code or through XML files. The following sections describe how to access these resources separately in two scenarios.
When the Android application is compiled to generate a class R, which contains the ID and all res / directory resources. You can use the class R, + by subclass resource name or directly use the resource ID to access resources.
Access res / drawable / myimage.png, and set it to the ImageView, you would use the following code:
ImageView imageView = (ImageView) findViewById(R.id.myimageview); imageView.setImageResource(R.drawable.myimage);
The first line of code in here to get R.id.myimageview defined myimageview of ImageView in the layout file. The second line with R.drawable.myimage to get the image in res / drawable in a subdirectory called myimage.
Consider an example in which res / values / strings.xml defined as follows:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello, World!</string> </resources>
Now you can use the ID of the object msg of TextView resource ID to set the text, as follows:
TextView msgTextView = (TextView) findViewById(R.id.msg); msgTextView.setText(R.string.hello);
Consider the following definition of 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>
The application code will load the activity layout, onCreate () method as follows:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="opaque_red">#f00</color> <string name="hello">Hello!</string> </resources>
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" />
Now, you can use these resources in the following layout file to set the text color and text content:
<code><?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" /> </code>
Now, if you return to the previous section to explain the "Hello World!" Instance, I can confirm you in this section all have a better understanding of the concept. Therefore, I strongly recommend to go back to look at the previous examples, and I use a different view of the basic resource usage.