Site icon DED9

Training to organize and access Android resources

There are many items you can use to build a good Android app. Apart from coding the program, you also maintain various other resources such as static content, bitmaps, colors, layout definitions, user interface strings, animation instructions, and more. 

These resources are always stored separately under different res / project categories.

This tutorial explains how you can organize application resources, identify alternative resources, and access them in your applications.

Organize resources in Android Studio

MyProject /

app /

manifest /

AndroidManifest.xml

java /

MyActivity.java

res /

drawable /

icon.png

layout /

activity_main.xml

info.xml

values ​​/

strings.xml

 

Number Directory and source type
1 / animXML files that define specific animations. They are stored in the res / anim / folder and are accessible from the R.anim class.
۲ / colorXML files that specify a list of colors. Are stored in res / color / and are accessible from the R.color class.
3 / drawableImage files such as .png, .jpg, .gif, or XML files embedded in bitmaps, mode lists, shapes, and animations. They are stored in res / drawable / and are accessible from the R.drawable class.
4 / layoutXML files that define a user interface layout. They are stored in res / layout / and are accessible from the R.layout class.
5 / menuXML files that define application menus, such as the settings menu, list, or submenu. They are stored in res / menu / and are accessible from the R.menu class.
6 / rawCustom files to save in their raw form. To open such raw files, you must call Source.openRawResource () with the source ID, which is R.raw.filename.
7 / valuesXML files that contain simple values ​​such as strings, integers, and colors. For example, here are some file naming rules for resources you can create in the directory –
  • arrays.xml for defining arrays are available from the R.array class.
  • integers.xml for defining integers are available from the R.integer class.
  • bools.xml for boolean definition, and are available from the R.bool class.
  • colors.xml for color definition and are available from the R.color class.
  • dimens.xml to define dimensions, and are available from the R.dimen class.
  • strings.xml are available to define strings from the R.string class.
  • styles.xml for defining styles, and are available from the R.style class.
8 / xmlCustom XML files that can be called at runtime by visiting Resources.getXML (). You can save the various configuration files used at runtime here.

Alternative resources

Your application must provide additional resources to support specific device settings. For example, you should consider removable alternative sources (such as images) for different resolution screens and alternative string sources for different languages. At runtime, Android detects the current device configuration and loads the appropriate resources for the application.

Follow these steps to create specific configuration options for a set of resources –

The following example shows images for a default screen and alternative images for a high-resolution screen.

MyProject /

app /

manifest /

AndroidManifest.xml

java /

MyActivity.java

res /

drawable /

icon.png

background.png

drawable-hdpi /

icon.png

background.png

layout /

activity_main.xml

info.xml

values ​​/

strings.xml

The following example specifies the layout for the default language and the alternative layout for the Arabic language.

MyProject /

app /

manifest /

AndroidManifest.xml

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

Access to resources

During application development, you must access the resources defined either in your code or in XML files. The following section describes how to access resources in both scenarios –

Access resources in code

When the Android app is compiled, an R class is generated that contains the source ID for all resources in res /. You can use the R class to access the source and its subcategories.

Example

Use the following code to access res / drawable / myimage.png and configure ImageView –

ImageView imageView = (ImageView) findViewById (R.id.myimageview);

imageView.setImageResource (R.drawable.myimage);

The first line of code uses R.id.myimageview to capture an ImageView with the myimageview ID defined in a layout file. The second line of code uses R.drawable.myimage to access an image named myimage below the drawable list in res /.

Example

In the next example res / values ​​/ strings.xml has the following definition –

<? xml version = ”1.0 ″ encoding =” utf-8 ″?>

<resources>

<string name = ”hello”> Hello, World! </string>

</resources>

You can now set the text on a TextView object using the msg tag below –

TextView msgTextView = (TextView) findViewById (R.id.msg);

msgTextView.setText (R.string.hello);

Example

Consider the layout in res / layout / activity_main.xml with the following definition –

<? 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 loads a layout for Activity using the onCreate () method –

public void onCreate (Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

setContentView (R.layout.activity_main);

}

Access resources in XML

Consider the xml file in res / values ​​/ strings.xml, which contains a color source and a string source –

<? xml version = ”1.0 ″ encoding =” utf-8 ″?>

<resources>

<color name = ”opaque_red”> # f00 </color>

<string name = ”hello”> Hello! </string>

</resources>

You can now use these resources in the following layout file to set the text color and text string as follows –

<? 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 to the previous season once again where Hello World! We are sure that you will have a better understanding of all the concepts described in this chapter. So we recommend that you review the previous chapter as an example and see how we used different resources at a very basic level.

Exit mobile version