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.
Related posts:
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=62e34ae6-3382-45cb-b4f5-f08a367c4a80)

Hello, I try your code, but I don’t see the alert when I click on the link. First alert is showing. Is there any bug?
Hi! I’m not sure what the problem could be. I compiled the example code again and it works for me on Android – the alert() shows up on every click. But it is true that it didn’t work in Firefox. I changed the demo HTML page a bit so that the alert dialog is now called from the onclick event. Can you try it again to see if it works?
Thanks alex,
now, in Firefox, the page work correctly. But I have still problem in my one project and i don’t know where is problem. But when I try it in new project, everything work all right. Thank you very much.
Hi, I have this code:
@Override
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
// some code to start new Activity
startActivity(i);
return true;
};
My link :
s += “” + “text“;
webview.loadDataWithBaseURL(null, ” ” + “” + “Title” +
+ “” + “” + s + “”
+ “”, “text/html”, “UTF-8″, “about:blank”);
So when I clik to the link on displej (with my finger) new Activity is open. It is all right. But when I use trackbol it open webbrowser. Why?
Unfortunately, I don’t know the answer to your question. I did some quick tests and I can’t get clicking with trackball to behave differently from clicking with your finger. Perhaps developers on one of Android mailing lists will be able to help you out: http://developer.android.com/community/index.html
Hi. I have still problem with scrolling. When I click on the link and alert return true, web page scroll top. I can forbid this scroll. Is there any solution? Can I react after the metod onJsAlert end?
Hi, I saw this on the apps-for-android repo as well, works perfectly.
What I would need is to do it with WebChromeClient instead of WebViewClient — the two are very similar but the former doesn’t have the onJsAlert.
Any ideas?
@paul: take a closer look. The example already uses a WebChromeClient which does have onJsAlert. :)
Sorry, I mean WebViewClient instead of WebChromeClient …