Android xml resource example 

Joined:
07/27/2010
Posts:
128

April 18, 2011 15:08:21    Last update: April 18, 2011 15:12:20
  1. Generate android project
    $ ~/android-sdk-linux_86/tools/android create project \
    > --package com.android.xmlres \
    > --activity XMLResource \
    > --target 2 \
    > --path XMLResource
    Created project directory: XMLResource
    Created directory /home/freyo/work/XMLResource/src/com/android/xmlres
    Added file XMLResource/src/com/android/xmlres/XMLResource.java
    Created directory /home/freyo/work/XMLResource/res
    Created directory /home/freyo/work/XMLResource/bin
    Created directory /home/freyo/work/XMLResource/libs
    Created directory /home/freyo/work/XMLResource/res/values
    Added file XMLResource/res/values/strings.xml
    Created directory /home/freyo/work/XMLResource/res/layout
    Added file XMLResource/res/layout/main.xml
    Created directory /home/freyo/work/XMLResource/res/drawable-hdpi
    Created directory /home/freyo/work/XMLResource/res/drawable-mdpi
    Created directory /home/freyo/work/XMLResource/res/drawable-ldpi
    Added file XMLResource/AndroidManifest.xml
    Added file XMLResource/build.xml
    


  2. Create XML file res/xml/books.xml:
    <?xml version="1.0"?>
    <catalog>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications 
          with XML.</description>
       </book>
       <book id="bk102">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former architect battles corporate zombies, 
          an evil sorceress, and her own childhood to become queen 
          of the world.</description>
       </book>
    </catalog>
    


  3. Edit layout (res/layout/main.xml):
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/booklist"
        />
    </LinearLayout>
    


  4. Edit code (src/com/android/xmlres/XMLResource.java):
    package com.android.xmlres;
    
    import java.io.*;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.content.Context;
    import android.content.res.XmlResourceParser;
    import android.widget.TextView;
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    
    public class XMLResource extends Activity
    {
        private static final String LOG_TAG = "XMLResource";
        private String booklist = "";
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
    	XmlResourceParser p = getResources().getXml(R.xml.books);
    
    	try {
    	    // skip ahead to the first start tag
    	    int nextEvent = p.next();
    	    while (nextEvent != XmlPullParser.START_TAG) {
    		nextEvent = p.next();
    	    }
    
    	    processTag(p, p.getName(), new TitleCollector());
    	}
    	catch (XmlPullParserException e) {
    	    booklist = "ERROR: Failed to parse XML!\n" + e;
    	}
    	catch (IOException e) {
    	    booklist = "ERROR: IOException!\n" + e;
    	}
    	finally {
    	    p.close();
    	}
    
    	// display book list
    	TextView t = (TextView) findViewById(R.id.booklist);
    	t.setText(booklist);
        }
    
        private void processTag(XmlPullParser p, String tag, TitleCollector tc)
    		 throws IOException, XmlPullParserException {
    	tc.collectTitle(p, tag);
    
    	int nextEvent = (p.getEventType() == XmlPullParser.END_TAG) ?
    			p.getEventType() :
    			p.next();
    	while (nextEvent != XmlPullParser.END_TAG) {
    	    if (nextEvent == XmlPullParser.START_TAG) {
    		processTag(p, p.getName(), tc);
    	    }
    	    nextEvent = p.next();
    	}
    	p.require(XmlPullParser.END_TAG, null, tag);
        }
    
        private class TitleCollector {
    	public void collectTitle(XmlPullParser p, String tag) 
    		    throws IOException, XmlPullParserException {
    	    if ("title".equals(tag)) {
    		booklist += p.nextText() + "\n";
    	    }
    	}
        }
    }
    


  5. Change activity label from app_name to booklist (AndroidManifest.xml):
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.android.xmlres"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:label="@string/app_name" android:icon="@drawable/icon">
            <activity android:name="XMLResource"
                      android:label="@string/booklist">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    


  6. Add value for string resource (res/values/string.xml):
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">XMLResource</string>
        <string name="booklist">Book List</string>
    </resources>
    


  7. Deploy and test:
    ant install
    



Share |
| Comment  | Tags