Page 1 of 1

Working with Resourses

#1 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Posted 18 April 2011 - 09:39 AM

Resources in Android

Table of contents:
  • Why is this important for me
  • What Resources does Android support
  • The Resources and how to access them
    • Text
    • Arrays
    • Pictures
    • Colors
    • Menu's
    • Layout's
    • Styles & Themes
    • Animations
    • XML-Configuration Data
    • Binary Data
    • Dimensions

  • The End


Why is this important for me
Resources are important to any Android-Developer. An Android-App comes as one file with everything needed in it. This means Bytecode but also other Resources like your Layouts, Texts and Pictures.
In Android, every resource has it's place, and this is where Android searches for them. So you need to know what you should put where and how. This is what you're (hopefully) going to learn in this Tutorial.

What Resources does Android support
One Resource should be familiar with all of you: The Layouts. Those are simple XML-Files, which contain the Layouts for you View's.
Most of you might also know about the String-Resources. This is the right place to put every text in, so your Application can be translated easily.

Android knows a lot of Resources and how to handle them. In the following Tutorial, I'm going to tell you:
  • Where to put the Resource
  • What Data-Formats are supported
  • How to define this Resource (if necessary)
  • How to access them with Android

All Resources belong into the res-Directory and then in their own sub-directory's. In Android, you have a great helper for all your Resources, that's the (auto generated) R.class. This class indexes and grants access to all defined Resources.

So, let's go!

The Resources and how to access them

Text
Where to put the Resource: Text-Resources belong in the res/values/strings.xml-file.
What Data-Formats are supported: XML

How to define this Resource (if necessary):
Strings (what Text basically is) are defined in XML-Format as Kay-Value pairs. This might look like this:
<?xml version="1.0" encoding="utf-8"?>
<Resources>
	<!-- Pre-defined Strings -->
    <string name="hello">Hello World!</string>
    <string name="app_name">Resources</string>
    <!-- Own Strings -->
    <string name="my_string">This is my first String!</string>
</Resources>


The XML-Attribute "name" identifies the String-Resource through the R.class-File. You can easily add you own Strings within the Resources-Tags.
You can also use HTML-Tags like <b></b> to format your Strings.

How to access them with Android:
In XML:
Strings can be accessed in XML (for example in your Layout-Definition) by simply using @string/[String-Name]. This might look like this:
[...]
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/my_string"
    />
[...]


In Java:
To access your Strings in Java (e.g. for dynamic Layouts) you can use the getString([String-Name])-method:
String str = this.getString(R.string.my_string);

Here you can see how the R.class works: The class contains an inner class called "string", which contains all the defined String-names. This is then used to find the value for that name.


Arrays
Where to put the Resource: In the res/values/arrays.xml-file.
What Data-Formats are supported: XML

How to define this Resource (if necessary):
The "arrays"-File contains (you won't guess it...) Array's. They are also defined in XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<array name="my_array">
		<item>First Item</item>
		<item>Second Item</item>
	</array>
</resources>



How to access them with Android:
In XML:
Doing a Layout-Definition, you can access the Array (e.g. to fill a Spinner) by the @ and the Array-Name:
[...]
<Spinner
 	android:layout_width="fill_parent"
 	android:layout_height="wrap_content"
 	android:entries="@array/my_array"
 	/>
[...]

Note that the Spinner doesn't support the entryValues-Attribute!
This will populate this Spinner with the entry's from the Array we created.

In Java:
To access the Array in Java, we'll need a ContextWrapper, which grant's access to all resources:
String[] str_arr = this.getResources().getStringArray(R.array.my_array);
// Also for Int-Arrays:
int[] int_arr = this.getResources().getIntArray(R.array.my_int_array);



Pictures
Where to put the Resource: /res/drawable e.g. /res/drawable-[mode]
What Data-Formats are supported: JPEG, PNG, Nine-Patch (.9.png)

How to define this Resource (if necessary):
There is no need to define those Resources (as they are simply binary files) but there is something special since Android 1.6:

To make your Android-App available to every Android Device, you need to think of different Screen-Resolutions. Since Android 1.6, Pictures are spited up in three Screen-Resolution-Groups:
  • /res/drawable-mdpi = HVGA (320x480 dpi)
  • /res/drawable-ldpi = QVGA (240x320 dpi)
  • /res/drawable-hdpi = WVGA (480x800 dpi)

You can use those directory's for optimized files on every resolution or put all Pictures in the old /res/drawable-folder. Your choice!

How to access them with Android:
You don't need to define a name or something for a picture, the file-name will be used as the identifier.

In XML:
For example, if you'd like to display an Image in a ImageView:
[...]
<ImageView
 	android:layout_width="fill_parent"
 	android:layout_height="wrap_content"
 	android:src="@drawable/android"
 />
[...]

As you can see, the Image in my case is named "android(.png)".

In Java:
Let's keep this example. If we'd like to change the picture shown by our ImageView, we can do this in the Java code (The ImageView is called "drawme"):
ImageView view = (ImageView) this.findViewById(R.id.drawme);
view.setImageResource(R.drawable.android);

The setImageResource()-method awaits simply the Resource of our picture, so we don't need to get it our self.


Colors
Where to put the Resource: res/values/colors.xml
What Data-Formats are supported: XML

How to define this Resource (if necessary):
Defining colors is done in easy XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<!-- To define a color, simply use Hex-
	Decimal Numbers	like known from HTML: -->
	<color name="error">#FF0000</color>
	<!-- Colors can also be Opacity
	(#AARRGGBB) while A is the Alpha-Canal -->
	<color name="nice">#F0053781</color>
</resources>



How to access them with Android:
In XML:
Simply use the name you gave the color:
[...]
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:background="@color/nice"
    android:textColor="@color/error"
    />
[...]


In Java:
To set the text color of our TextView in Java, we would do:
TextView tex = (TextView) this.findViewById(R.id.text);
tex.setTextColor( this.getResources().getColor(R.color.error) );



Menu
Where to put the Resource: res/menu/[Menu-Name].xml
What Data-Formats are supported: XML

How to define this Resource (if necessary):
Creating Menus for a View is also done in XML:
<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  
  <!-- We use the standard Android-Menu Icons for our
  Menu-Item. Those are in @android:drawable -->
  <item
  	android:id="@+id/first_item"
  	android:title="@string/my_string"
  	android:icon="@android:drawable/ic_menu_help"
  	/>
</menu>


A list with every available Android-Menu Icon can be found here (Menu-Item-Icons begin with "ic_").

How to access them with Android:
In XML:
Menus can't be accessed in XML.

In Java:
To add a Menu to a View, load it in the onCreateOptionsMenu()-method:
@Override
public boolean onCreateOptionsMenu(Menu menu){
    // Load the Menu:
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

The Menu is identified by it's file-name!


Layout
Where to put the Resource: res/layout/[Layout-Name].xml
What Data-Formats are supported: XML
How to define this Resource (if necessary):
I'm not going into this here, check out the "A Simple UI with Events" Tutorial by gabehabe.

How to access them with Android:
This is also explained in gabehabe's Tutorial above.


Styles & Themes
Where to put the Resource: res/values/styles.xml
What Data-Formats are supported: XML
How to define this Resource (if necessary):
This would be to much for this list, so I'll refer to Google's tutorial on Styles and Themes. This might be stuff enough for another Tutorial some day, we'll see ;)

How to access them with Android:
See the Tutorial linked above.


Animations
Where to put the Resource: res/anim/[Animation-Name].xml
What Data-Formats are supported: XML
How to define this Resource (if necessary):
With animations you can animate things on your Android UI like switching the View or something. This is a very complex topic, so I'll refer to another Tutorial here.

How to access them with Android:
See tutorial above.


XML-Configuration Data
Where to put the Resource: res/xml/[Datei].xml
What Data-Formats are supported: XML
How to define this Resource (if necessary):
There is no template on how to write those files, simply make your own XML File which stores the things you want to store:
<?xml version="1.0" encoding="UTF-8"?>
<root>
	<item>
		<title>Some News</title>
		<abstract>Some short Text about the News.</abstract>
		<link standalone="true">http://www.google.com</link>
	</item>
</root>


How to access them with Android:
To parse this XML-Data, we use the Android-Class XmlResourceParser. A short Tutorial on how to use it can be found here.


Binary Data
Where to put the Resource: res/raw/[Data]
What Data-Formats are supported: All
How to define this Resource (if necessary):
No need to define anything. Simply put binary-resources (like Videos or HTML-Help Pages) in here.

How to access them with Android:
To open raw-data like HTML or anything else that can easily be read, we use the openResource(id) e.g. the openResource(id, typeValue)-methods, which return an InputStream that we'll use to read them. Those are methods from the Resources-Class:
public String readData() throws IOException{
	BufferedReader in = new BufferedReader(
		new InputStreamReader(
			this.getResources().openRawResource(R.id.text)));
	StringBuilder str = new StringBuilder();
	String line;
	while ( (line = in.readLine()) != null){
		str.append(line);
	}
	return str.toString();
}

Movies and Music has it's own methods. Basically those are:
  • Musik = MediaPlayer-Class
  • Movies = getMovie(id)-Method



Dimensions
Where to put the Resource: res/values/dimens.xml
What Data-Formats are supported: XML
How to define this Resource (if necessary):
Dimensions are used to define a specified size. Units are:
  • dp, dip = Density-independent pixel
  • in = Inch
  • mm = Millimeter
  • pt = Points (or Pixels)
  • sp = Scale-independent pixel

A definition in XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<dimen name="my_dimension">12mm</dimen>
	<dimen name="my_second_dimension">12dp</dimen>
</resources>



How to access them with Android:
In XML:
<TextView  
    android:layout_width="@dimen/my_dimension" 
    android:layout_height="@dimen/my_second_dimension"
/>


In Java:
float dim = this.getResources().getDimension(R.dimen.my_dimension);


The End
Done. Hope you find this useful (maybe as a reference book or something like this).

Greetings: Luke

Is This A Good Question/Topic? 1
  • +

Replies To: Working with Resourses

#2 Jager686  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-October 13

Posted 05 March 2014 - 10:14 AM

thanks Luke for the tutorial, nice to have a refresher. Can you make a tutorial on some of the components or explain how to imply onDraw (paint, canvas, color, path) method?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1