Masochist
Masochist

Reputation: 399

Modify alert() title (Javascript in Android Webview)

Screenshot: The page at file://

Is there anyway to modify the alert box title? Any help will be greatly appreciated. :)

Upvotes: 9

Views: 8149

Answers (4)

user10013769
user10013769

Reputation:

There are 3 types of js alerts:

  • alert box - with an Ok button to proceed.
  • confirm box - with both OK and cancel button.
  • prompt box - get a value from the user and then select OK/Cancel.

  • Use onJsAlert for alertBox .
  • Use onJsConfirm for confirmBox .
  • Use onJsPrompt for promptBox

I have added the sample code for onJsConfirm,

        webViewLayout.setWebChromeClient(new WebChromeClient(){
        @Override
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {

            AlertDialog dialog =new AlertDialog.Builder(view.getContext()).
                    setTitle("Confirm").
                    setIcon(ContextCompat.getDrawable(view.getContext(),R.drawable.image)).
                    setMessage(message).
                    setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            result.cancel();
                        }
                    }).
                    setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            FlashMessage("JS");
                            result.confirm();
                        }
                    })
                    .create();
            dialog.show();
            return true;
        }
    });

Upvotes: 3

Frank021
Frank021

Reputation: 141

Yes its possible, i did that

   webview.setWebChromeClient(new WebChromeClient() {




        public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
        {
            new AlertDialog.Builder(activity)
            .setTitle("Calendário App...")
            .setMessage(message)
            .setPositiveButton(android.R.string.ok,
                    new AlertDialog.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int wicht)
                {
                    result.confirm();
                }
            }).setCancelable(false)
            .create()
            .show();
            return true;
        };
            });

Upvotes: 1

Masochist
Masochist

Reputation: 399

@Pointy says this is not possible due to the browser's security measure.

Upvotes: 0

Paulo Bueno
Paulo Bueno

Reputation: 2569

Indeed you can envelop it using the following code:

    final Context myApp=this;

    webView.setWebChromeClient(new WebChromeClient(){
    @Override
    public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
    {
        new AlertDialog.Builder(myApp)
        .setTitle("Simmon says...")
        .setMessage(message)
        .setPositiveButton(android.R.string.ok,
                new AlertDialog.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int wicht)
            {
                result.confirm();
            }
        }).setCancelable(false)
        .create()
        .show();
        return true;
    };
    });

Code source here

gl

Upvotes: 14

Related Questions