Monday, January 23, 2012

How to Use HTML, CSS and JavaScript in Android

1. Create project called "htmlIntent"
2. create activity "WebviewExample"
3. in assets folder create a folder named "www"
4. create "local.html" file inside www folder
5. create a activity called "WebviewOtherExample"
6. manifest file
see below to details -



public class WebviewExample extends Activity{
WebView webview;
private Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = new WebView(this);
setContentView(webview);

WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public boolean onJSAlert(WebView view, String url, String message, JsResult result)
{
return true;
}
});
webview.addJavascriptInterface(new JSInterface(), "jsinterface");
webview.loadUrl("file:///android_asset/www/local.html");
}

public void clickTest(){
Intent nintent = new Intent(WebviewExample.this,WebviewOtherExample.class);
startActivityForResult(nintent,0); // if we did startActivityForResult we could get the data back??
}
public class JSInterface {

public void specialClick() {
handler.post(new Runnable() {
public void run() {

Intent nintent = new Intent(WebviewExample.this,WebviewOtherExample.class);
startActivityForResult(nintent,0); // if we did startActivityForResult we could get the data back??
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
//Bundle extras = intent.getExtras();
switch(requestCode)
{
case 0:
if (resultCode == RESULT_OK)
{
Log.v("WEBVIEW","Got Results");
}
break;
}
}

}

3. in assets folder create a folder named "www"
4. create "local.html" file inside www folder




Click from HTML





Hello Ready to Call Intent


Click Me


-->


5. create a activity called "WebviewOtherExample"

public class WebviewOtherExample extends Activity {
Button aButton;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
aButton = (Button) findViewById(R.id.Button01);
aButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
WebviewOtherExample.this.finish();

}
});
}
}
7. manifest file



package="com.mobvcasting.webviewexample"
android:versionCode="1"
android:versionName="1.0" >

android:icon="@drawable/icon"
android:label="@string/app_name" >
android:label="@string/app_name"
android:name=".WebviewExample" >