Tuesday, 20 April 2010

Read XML Resources in Android, using XmlResourceParser: XML parsing interface

XmlResourceParser is the XML parsing interface returned for an XML resource. This is a standard XmlPullParser interface, as well as an extended AttributeSet interface and an additional close() method on this interface for the client to indicate when it is done reading the resource.

In this exercise, we read our own XML Resource (in /res/xml folder) using XmlResourceParser, and display the contents on screen.



First of all, create a folder /res/xml, and our own XML file myxml.xml
<?xml version="1.0" encoding="utf-8"?>
<rootelement1>
<subelement>
Hello XML Sub-Element 1
</subelement>
<subelement>
Hello XML Sub-Element 2
<subsubelement>Sub Sub Element</subsubelement>
</subelement>
</rootelement1>


Modify 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:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/my_xml"
/>
</LinearLayout>


Finally, modify java code
package com.exercise.AndroidXmlResource;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidXmlResource extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView myXmlContent = (TextView)findViewById(R.id.my_xml);
String stringXmlContent;
try {
stringXmlContent = getEventsFromAnXML(this);
myXmlContent.setText(stringXmlContent);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private String getEventsFromAnXML(Activity activity)
throws XmlPullParserException, IOException
{
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.myxml);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
stringBuffer.append("--- Start XML ---");
}
else if(eventType == XmlPullParser.START_TAG)
{
stringBuffer.append("\nSTART_TAG: "+xpp.getName());
}
else if(eventType == XmlPullParser.END_TAG)
{
stringBuffer.append("\nEND_TAG: "+xpp.getName());
}
else if(eventType == XmlPullParser.TEXT)
{
stringBuffer.append("\nTEXT: "+xpp.getText());
}
eventType = xpp.next();
}
stringBuffer.append("\n--- End XML ---");
return stringBuffer.toString();
}
}


Download the files.

Related Article: A simple RSS reader, using Android's org.xml.sax package.



Monday, 19 April 2010

Turning a Blogger Blog into a Website

Google LogoAnother quick note. The dudes at Magic with Blogs have created a site with a number of Blogger video tutorials. If you look down at the bottom of their home page, you will notice a section on how to convert of a Blogger blog into a web site. The post on making your home page static is key.



This is an idea that will be useful for a lot of people. A quick way to setup a Website site using the best site/blog creation software on the net. Expect a few posts from me on the subject. :)

Changing Your Blogger Account and Housekeeping

Over the weekend, I did a little housekeeping on this site. I discovered that you can actually change the Google account you use to host your blog. I have done that so I will be updating the about pages and stuff with the new information. Now my twitter and buzz accounts should have a little be more consistent branding. In addition, my about and contact information will be moving to my blogger account using the new static pages feature of Blogger.



Update: Note that once you do this, you have to go to Settings -> Basic and change to the new editor if you were using that on the old blog. The same goes for any other updated features I assume.

Friday, 16 April 2010

How to define dimension in Android using XML

You can create common dimensions to use for various screen elements by defining dimension values in XML.

Here is some examples to define dimension using XML.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="one_pixels">1px</dimen>
<dimen name="five_dp">5dp</dimen>
<dimen name="1_inch">1in</dimen>
<dimen name="1_cm">10mm</dimen>
</resources>


Thursday, 15 April 2010

Media Wiki on a Stick

Want to run Media Wiki on your local machine?  Well the basic steps are described here. You basically have to download and install the MoWeS Portable server along with Media Wiki. You run the installer on your usb drive. Boom, you have MediaWiki on a USB Drive.



As a caveat, this sort of thing is not for the feint of heart. :) A basic understanding of the Apache web server is required.

Wednesday, 14 April 2010

Wanna Learn Chinese?

Wanna learn Chinese?  Get started learning Chinese with Peggy Lee's You Tube channel. She also does private Chinese tutoring via skype.



What a great idea for a business!

Android - Apps can easily embed the web


Watch Android engineers demonstrate that applications on Android can easily embed the web. Apps can easily embed HTML, Javascript, and style sheets. An app can render web content through a WebView.