As promised earlier, here is an example of how to add support for alert() function to a WebView in your Android application:
final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);
final Context myApp = this;
/* WebChromeClient must be set BEFORE calling loadUrl! */
browser.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
{
new AlertDialog.Builder(myApp)
.setTitle("javaScript dialog")
.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;
};
});
/* load a web page which uses the alert() function */
browser.loadUrl("http://lexandera.com/files/jsexamples/alert.html");
Code for adding support for confirm() and prompt() is almost identical and can be found in Mosembro’s source code.
Tags: Android, JavaScript, WebChromeClient, WebView
My blog stats are telling me that many visitors are arriving here looking for examples on how to implement WebChromeClient on Android. I don’t (yet) have a nice article about how to do it, but you can have a look at how I implemented WebChromeClient in Mosembro, if you came here for that reason. Just use your browser’s search functionality and look for “new WebChromeClient”.
Tags: Android, browser, example, WebChromeClient, WebView
Back in early ‘08, while the Androd developer challenge was in full swing, a fairly common question which went without a satisfactory answer was: “How can I manipulate the contents of a WebView?”. I now finally have the answer, thanks to a couple of lines of code I stumbled upon while looking at the source of Jeffrey Sharkey’s OilCan.
The solution is actually very simple: wait for the original page to finish loading, then inject your own JavaScript code into it by calling webview.loadUrl(“javascript:your-code-here”) when your webvew’s onPageFinished() event is triggered. Loading an URL beginning with “javascript:” is the exact same approach used by bookmarklets.
Here’s a simple example which loads a page and then sets its text color to red:
final WebView webview = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
webview.getSettings().setJavaScriptEnabled(true);
/* WebViewClient must be set BEFORE calling loadUrl! */
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
webview.loadUrl("javascript:(function() { " +
"document.getElementsByTagName('body')[0].style.color = 'red'; " +
"})()");
}
});
webview.loadUrl("http://code.google.com/android");
That’s it.
Please note though that alert() and confirm() JavaScript functions will not work unless you also register a WebChromeClient which implements the required methods.
Tags: Android, JavaScript, WebChromeClient, WebView, WebViewClient