Skip to content

Instantly share code, notes, and snippets.

@sandeepyohans
Last active October 30, 2023 10:42
Show Gist options
  • Save sandeepyohans/ce6c5cc596f693733255c3448a896247 to your computer and use it in GitHub Desktop.
Save sandeepyohans/ce6c5cc596f693733255c3448a896247 to your computer and use it in GitHub Desktop.
Adding alert() support to a WebView - Android
/*
Retrieved from https://web.archive.org/web/20160516165158/http://lexandera.com/2009/01/adding-alert-support-to-a-webview/
*/
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");
@rubo77
Copy link

rubo77 commented May 7, 2019

If you get the error that Context and AlertDialog can not be resolved, you need to

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

@tiagocaus
Copy link

Erro: http://prntscr.com/nn0oee

package br.com.aaaaa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        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");


    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment