Android has a special folder called res, it makes easier to accessing android resources. Using resources you can provide different characteristics to your application without modifying your code. Different dimensions, layouts, image sizes, languages, etc…
Accessing Android resources
Resource Types
The resource folder supports some special subdirectories.
Directory | Resource Type |
---|---|
animator | XML files that define property animations |
anim | XML files that define tween animations |
color | XML files that define a static list of colors |
drawable | Bitmap files (.png, .9.png, .jpg, .gif) or XML files |
layout | XML files that define a user interface layout |
menu | XML files that define application menus |
raw | Arbitrary files to save in their raw form |
values | XML files that contain simple values |
xml | Arbitrary XML files that can be read at runtime |
See more on the Android website
R.java
When you put a file in a resource folder, the ide will automatically add it to a special class named R (This class is auto-generated. NEVER change it). By default, it will create something like R.directory.filename
(without extension), except for the files inside the folders andvalues
color
. For those, the IDE will use the tags inside this file. For example:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">ATest</string> <string-array name="gender"> <item>Female</item> <item>Male</item> </string-array> <dimen name="app_padding">10dp</dimen> </resources>
In this case ,the IDE creates three entries on the R file: R.string.app_name
, R.array.gender
and R.dimen.app_padding
Accessing
XML
For accessing resources inside your XML you can use: @<resource_type>/<resource_name>
<?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/app_padding"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_name" /> </LinearLayout>
Java
For accessing resources inside your classes you can use: R.<resource_type>.<resource_name>
Some methods just expect a Resource Id to be provided, like:
setContentView(R.layout.xpto); myTextView.setText(R.string.app_name)
Sometimes you need to get the content of your resource. Context and Activity have methods to access resources
getResources().get<resource_type>(R.<resource_type>/<resource_name>);
public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String appName = getResources().getString(R.string.app_name); String[] gender = getResources().getStringArray(R.array.gender); } }
For more details take a look at Android documentation page Accessing Resources or in this example app