Jan 27 2009

Adding alert() support to a WebView

Category: Android,JavaScript,WebView examplesAleksander Kmetec @ 9:54 pm

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 */ 

final Context myApp = this;

/* WebChromeClient must be set BEFORE calling loadUrl! */
browser.setWebChromeClient(new WebChromeClient() {
    public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) 
        new AlertDialog.Builder(myApp)
            .setTitle("javaScript dialog")
                    new AlertDialog.OnClickListener() 
                        public void onClick(DialogInterface dialog, int which) 
        return true;

/* load a web page which uses the alert() function */

Code for adding support for confirm() and prompt() is almost identical and can be found in Mosembro’s source code.

Reblog this post [with Zemanta]

Tags: , , ,

9 Responses to “Adding alert() support to a WebView”

  1. Sikus

    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?

  2. alex

    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?

  3. Sikus

    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.

  4. Sikus

    Hi, I have this code:
    public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
    // some code to start new Activity
    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?

  5. Aleksander Kmetec

    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

  6. Sikus

    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?

  7. paul

    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?

  8. Aleksander Kmetec

    @paul: take a closer look. The example already uses a WebChromeClient which does have onJsAlert. :)

  9. paul

    Sorry, I mean WebViewClient instead of WebChromeClient …