Resources are static bits is held outside the Java source code. Have you ever seen one kind of resources - layouts - often appear in the examples in this course. There are many different types of resources, such as images and strings, you can use them in your Android applications.
Resources types
In addition to the layouts resource, there are many other resources available to you, including the following types:
Directory
|
Resource
Type
|
anim/
|
XML
files that define property animations. They are saved in res/anim/ folder and
accessed from the R.anim class.
|
color/
|
XML
files that define a state list of colors. They are saved in res/color/ and
accessed from the R.color class.
|
drawable/
|
Image
files like .png, .jpg, .gif or XML files that are compiled into bitmaps,
state lists, shapes, animation drawable. They are saved in res/drawable/ and
accessed from the R.drawable class.
|
layout/
|
XML
files that define a user interface layout. They are saved in res/layout/ and
accessed from the R.layout class.
|
menu/
|
XML
files that define application menus, such as an Options Menu, Context Menu,
or Sub Menu. They are saved in res/menu/ and accessed from the R.menu class.
|
raw/
|
Arbitrary
files to save in their raw form. You need to callResources.openRawResource() with
the resource ID, which isR.raw.filename to open such raw files.
|
values/
|
XML
files that contain simple values, such as strings, integers, and colors. For
example, here are some filename conventions for resources you can create in
this directory:
|
xml/
|
Arbitrary
XML files that can be read at runtime by calling
Resources.getXML() . You can save various configuration files here which will be used at run time. |
Now, I would like to introduce some important resources.
Strings Resource
res/strings.xml
file. This file is auto-generated after we creating a new Android Studio project. Each string declared by <string>
tag. For example:
strings.xml
After defining it like above, you can use them both xml layouts and programmatically code. In xml, use it with <resources>
<string name="app_name">Android Resources</string>
<string name="demo">DEMO String</string>
</resources>
@string/[string_name]
:
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/demo" />
Context
(usually is your Activity
), with getResources().getString()
, you can use these strings resource:
TextView textView = (TextView)findViewById(R.id.text);
textView.setText(this.getResources().getString(R.string.demo)); // set TextView content
Images Resource
Drawable
object, such as an image or a background of an ImageView
.Using images resource is putting your photos into the "res/drawable" folder and then refer to them as a resource. From within the layout files, the images are referenced through
@drawable/[image_name]
(e.x: with @drawable/[image_name]
, the name of the resource will be @drawable/foo
). In Java, when you need a photo ID of a resource, just use R.drawable.[image_name]
(e.x: R.drawable.foo
).For example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@drawable/green_dot" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_call"
android:text="@string/call" />
</LinearLayout>
Like strings resource, you can use these drawables in Java code, this code is equivalent with xml defining above:
imageView.setImageResource(R.drawable.green_dot); // set Image for ImageView
button.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_call, 0, 0, 0); // set left drawable for Button
Colors resource
The colors in Android valuable in HEX format with an additional option to specify an alpha channel. You can select the type of a character value 16 or value 16 type 2 characters, there are 4 styles as below:
- #RGB
- #ARGB
- #RRGGBB
- #AARRGGBB
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
You can use theme like strings or images resources.Styles/Themes resource
A special resource type in Android, which styling objects, define the look and format for UI elements or further, declaring theme for your app. For details about this, you can read at next chapter: Styles and Themes in Android.
Conclusions
In this tutorial, we looked at the basics of app resources on Android. But as you have seen, there is much more to explore. So, you can go to the official page of Android SDK to deep understanding about
Resources
. Moreover, you can read this page to know details of all resources types.