Add JS-Alert and other Js-Function Support to Android Webview

by Sasikumar 2013-10-25 21:01:55

To add js-functions support to a WebView in android:
In order to generate android alert in webview while a js alert is generated we can use the following method
WebView myWebView = (WebView)findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true); /* Enable Javascript to make it function */
final Context mContext = this;
/* WebChromeClient must be set before loading the url using loadUrl */
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
{
new AlertDialog.Builder(mContext)
.setTitle("Title-Alert")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setCancelable(false)
.create()
.show();

return true;
};
});

myWebView.loadUrl("http://www.hiox.org/"); /* Call the webpage */

This helps in making response to a javascript alert generated from a webpage.

Similarly we can also add other function supports to the webview as follows
For onPageFinished
@Override
public void onPageFinished(WebView view, String url)
{

}

For onPageStarted
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
}

For onJsConfirm
@Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result)
{
new AlertDialog.Builder(mContext)
.setTitle("javaScript dialog")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.cancel();
}
})
.create()
.show();

return true;

}

For onJsPrompt
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,final JsPromptResult result)
{
new AlertDialog.Builder(mContext)
.setTitle("javaScript dialog")
.setView(v)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = "testexample"
result.confirm(value);
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
result.cancel();
}
})
.setOnCancelListener(
new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
result.cancel();
}
})
.show();

return true;
};

For onProgressChanged
@Override
public void onProgressChanged(WebView view, int newProgress)
{
updateProgress(newProgress);
super.onProgressChanged(view, newProgress);
}

For onReceivedTitle
@Override
public void onReceivedTitle(WebView view, String title)
{
setTitle(title);
super.onReceivedTitle(view, title);
}

This would help in making control over the JS functions in Android.
999
like
0
dislike
0
mail
flag

You must LOGIN to add comments