by aryalsambit » Thu Dec 30, 2010 9:31 am
Hello!!
Here'z my code.
Show_Info.java
package com.example.java;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Show_Info extends Activity {
Transformer trans;
WebView browser;
TextView text;
Button btnSho;
Button btnsave;
private static final String TAG = "LocalBrowser";
private final Handler handler = new Handler();
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InputStream fos;
InputStream fof;
try {
fos = getResources().openRawResource(R.raw.xml);
Source xmlSource = new StreamSource(fos);
fof = getResources().openRawResource(R.raw.xsl);
Source xsltSource = new StreamSource(fof);
StringWriter vwriter = new StringWriter();
StreamResult result = new StreamResult(vwriter);
TransformerFactory transFact = TransformerFactory.newInstance();
trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource,result);
String document = vwriter.toString();
btnSho=(Button)findViewById(R.id.btnshow);
btnsave=(Button)findViewById(R.id.save);
text = (TextView) findViewById(R.id.text_view);
browser=(WebView)findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.addJavascriptInterface(new AndroidBridge(),"android");
browser.setWebChromeClient(new WebChromeClient());
browser.loadData(document,"text/html", "UTF-8");
} catch (Exception e2) {
// TODO Auto-generated catch blocks
e2.printStackTrace();
}
}
private class AndroidBridge {
public void callAndroid(final String arg) { //must be final
handler.post(new Runnable() {
public void run() {
Log.d(TAG, "callAndroid(" + arg + ")");
text.setText(arg);
}
});
btnSho.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Log.d(TAG, "onClick(" + view + ")");
browser.loadUrl("javascript:callJS()");
}
});
btnsave.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),
"Entry has been saved ",
Toast.LENGTH_SHORT).show();
browser.loadUrl("javascript:call()");
// HtmlElementCollection htmlElements = webBrowser1.Document.All;
}
});
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:id="@+id/btnshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:layout_x="1px"
android:layout_y="5px"
android:gravity="center"
/>
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btnshow"
android:layout_alignTop="@id/btnshow"
android:text="Save" />
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnshow"
android:layout_x="5px"
android:layout_y="5px">
</WebView>
</RelativeLayout>
xml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
<catalog>
<cd>
<title>
</title>
<artist>
</artist>
<country>
</country>
<company>
</company>
<price>
</price>
<year>
</year>
</cd>
</catalog>
xsl.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<script type="text/javascript"></script>
<script language="Javascript"><![CDATA[
function callJS() {
alert('hello world');
}
function call(){
var x=document.getElementById("form1");
for (var i=0;i<x.length;i++)
{
document.write(x.elements[i].value);
document.write("
");
}
}
]]></script>
</head>
<body onload="window.android.callAndroid('Hello from Browser')">
<form id="form1">
<h2>My CD Collection</h2>
<table border="1">
<xsl:for-each select="catalog/cd">
<tr>
<td>Title</td>
<td><xsl:value-of select="title"/><input type="text" id="title" name="#cd\title#" value="{title}"/></td>
</tr>
<tr>
<td>Artist</td>
<td><xsl:value-of select="artist"/><input type="text" id="artist" name="#cd\artist#" value="{artist}"/></td>
</tr>
<tr>
<td>Country</td>
<td><xsl:value-of select="country"/><input type="text" id="country" name="#cd\country#" value="{country}"/></td>
</tr>
<tr>
<td>Company</td>
<td><xsl:value-of select="company"/><input type="text" id="company" name="#cd\company#" value="{company}"/></td>
</tr>
<tr>
<td>Price </td>
<td><xsl:value-of select="price"/><input type="text" id="price" name="#cd\price#" value="{price}"/></td>
</tr>
<tr>
<td>Year</td>
<td><xsl:value-of select="year"/><input type="text" id="year" name="#cd\year#" value="{year}"/></td>
</tr>
</xsl:for-each>
</table>
</form>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Instead of showing values of text box in webkit, i want to save those values in xml.xml file or is there any method to create another xml file?
With Best Regards,
Thanking
Sambit