Webview is primarily used for loading a html content  in our application.
Let us discuss this in several parts. In this first part let load a normal web page toapplication using webview.
loadUrl is  the prime method to load a particular webpage to webview.
Hence make use of the snippet to load a page.
WebViewExample.java
[sourcecode language="java"]
public class WebViewExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.androidpeople.com");
webView.setWebViewClient(new HelloWebViewClient());
}
}
[/sourcecode]
But the above method loads the browser instead of opening the webpage inside the application.
To overcome this difficulty we should use the method shouldOverrideUrlLoading() along with webview and url string.
Now the application opens the web page inside the application itself.
[sourcecode language="java"]
public class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
[/sourcecode]
}
Layout design
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:padding="10px">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="AndroidPeople.com - Webview " android:textStyle="bold" android:textSize="20sp" android:textColor="#fff"></TextView><WebView android:id="@+id/webview" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
[/sourcecode]
Snapshot
webview-androidpeople example


Here we are going to see about how to parse a XML using SAX Parser.
we are going to parse XML from net not from local file.

Files Used:-

XMLParsingExample.java ( Main Activity )
SitesList.java ( Getter & Setter Method )
MyXMLHandler.java ( XML Handling )
example.xml ( XML file from net )

The output will looks similar to



[sourcecode language="xml"]


AndroidPeople
www.androidpeople.com


iPhoneAppDeveloper
www.iphone-app-developer.com


[/sourcecode]
If tag names are different, then we can use string to set & get the value. But here item,name & website tags are repeating 2 times. So we can use ArrayList to store & get the data. XMLParsingExample.java This is main activity class. when App. starts this file will be called first. This file contains how to use SAX Parser to handle XML tags.

package com.androidpeople.xml.parsing;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class XMLParsingExample extends Activity {
/** Create Object For SiteList Class */

SitesList sitesList = null;
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/** Create a new layout to display the view */

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);

/** Create a new textview array to display the results */

TextView name[];
TextView website[];
TextView category[];

try {

/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");

/** Create handler to handle XML Tags ( extends DefaultHandler ) */

MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}

/** Get result from MyXMLHandler SitlesList Object */

sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
name = new TextView[sitesList.getName().size()];
website = new TextView[sitesList.getName().size()];
category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}


/** Set the layout view to display */

setContentView(layout);
}
}
MyXMLHandler.java This file is used to handle the XML tags. So we need to extends with DefaultHandler. we need to override startElement, endElement & characters method . startElemnt method called when the tag starts. endElemnt method called when the tag ends characres method to get characters inside tag.
package com.androidpeople.xml.parsing;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = null;
public static SitesList sitesList = null;
public static SitesList getSitesList() {
return sitesList;
}
public static void setSitesList(SitesList sitesList) {
MyXMLHandler.sitesList = sitesList;
}
/** Called when tag starts ( ex:- AndroidPeople
* --  )*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("maintag"))
{
/** Start */
sitesList = new SitesList();
} else if (localName.equals("website")) {
/** Get attribute value */
String attr = attributes.getValue("category");
sitesList.setCategory(attr);
}
}
/** Called when tag closing ( ex:- AndroidPeople
* --  )*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("name"))
sitesList.setName(currentValue);
else if (localName.equalsIgnoreCase("website"))
sitesList.setWebsite(currentValue);
}
/** Called to get tag characters ( ex:- AndroidPeople
* -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}

SitesList.java

Contains Getter & Setter Method

[sourcecode language="java"]
package com.androidpeople.xml.parsing;
import java.util.ArrayList;
/** Contains getter and setter method for varialbles */
public class SitesList {
/** Variables */
private ArrayList name = new ArrayList();
private ArrayList website = new ArrayList();
private ArrayList category = new ArrayList();
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList getName() {
return name;
}
public void setName(String name) {
this.name.add(name);
}
public ArrayList getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website.add(website);
}
public ArrayList getCategory() {
return category;
}
public void setCategory(String category) {
this.category.add(category);
}
}