import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.TextView;
public class ImagesDemo extends Activity {
EditText name;
TextView result;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
name = (EditText)findViewById(R.id.name);
result = (TextView)findViewById(R.id.result);
ImageButton btn = (ImageButton)findViewById(R.id.format);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
applyFormat();
}
});
}
private void applyFormat() {
String format = getString(R.string.funky_format);
String simpleResult = String.format(format,
TextUtils.htmlEncode(name.getText ().toString()));
result.setText(Html.fromHtml (simpleResult));
}
}
Now, our button has the desired icon (see Figure 19-3).

Figure 19-3.
XML: The Resource Way
In Chapter 18, we showed how you can package XML files as raw resources and get access to them for parsing and usage. There is another way of packaging static XML with your application: the XML resource.
Simply put the XML file in res/xml/
, and you can access it by getXml()
on a Resources object, supplying it a resource ID of R.xml.
plus the base name of your XML file. So, in an activity, with an XML file of words.xml
, you could call getResources().getXml (R.xml.words)
.
This returns an instance of the currently-undocumented XmlPullParser
, found in the org.xmlpull.v1
Java namespace. Documentation for this library can be found at the parser’s site[17] as of this writing.
An XML pull parser is event-driven: you keep calling next()
on the parser to get the next event, which could be START_TAG
, END_TAG
, END_DOCUMENT
, etc. On a START_TAG
event, you can access the tag’s name and attributes; a single TEXT
event represents the concatenation of all text nodes that are direct children of this element. By looping, testing, and invoking per-element logic, you parse the file.
To see this in action, let’s rewrite the Java code for the Files/Static
sample project to use an XML resource. This new project, Resources/XML
, requires that you place the words.xml
file from Static
not in res/raw/
, but in res/xml/
. The layout stays the same, so all that needs replacing is the Java source:
package com.commonsware.android.resources;
import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class XMLResourceDemo extends ListActivity {
TextView selection;
ArrayList<String> items = new ArrayList<String>();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection = (TextView)findViewById(R.id.selection);
try {
XmlPullParser xpp = getResources().getXml (R.xml.words);