JavaScript alert not working in Android WebView

The question:

In my application I am using WebView and in that I am using JavaScript alert( ) method but its not working, no pop-up appears.

in my manifest file I have added

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

and in activity file I have added

mWebView = (WebView) findViewById(;

In layout xml file I have added


Any clue how to enable full JavaScript in WebView?


Thanks mark
the alert() method in the html file are working now 🙂 .

Now there are two issues in WebView :
1: I am using a <textarea> in the html file that i am loading in WebView , and trying to write in Hindi language font in it, but when i try to write Hindi text it displays as symbols ( rectangle symbols like [] ) .

when i do the same in firefox browser on desktop it works fine.
any clue how to give support for multiple language in textarea in WebView ?

2: When I am clicking submit and trying to open the value of text in alert() method in another java script it doesn’t work , does it mean even after using WebChromeClient
its applicable only for current loaded html page and not javascripts called from that page ?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

As others indicated, setting the WebChromeClient is needed to get alert() to work. It’s sufficient to just set the default WebChromeClient():

mWebView.setWebChromeClient(new WebChromeClient());

Thanks for all the comments below. Including John Smith’s who indicated that you needed to enable JavaScript.

Method 2

Check this link , and last comment , You have to use WebChromeClient for your purpose.

Method 3

webView.setWebChromeClient(new WebChromeClient() {
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        return super.onJsAlert(view, url, message, result);

Method 4

The following code will work:

private WebView mWebView;
final Activity activity = this;

// private Button b;
public void onCreate(Bundle savedInstanceState) {
    mWebView = (WebView) findViewById(;
    mWebView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            activity.setProgress(progress * 1000);


Method 5

You can try with this, it worked for me

WebView wb_previewSurvey=new WebView(this); 

       wb_previewSurvey.setWebChromeClient(new WebChromeClient() {
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            //Required functionality here
            return super.onJsAlert(view, url, message, result);


Method 6

if you wish to hide URL from the user, Show an AlertDialog as below.

myWebView.setWebChromeClient(new WebChromeClient() {

            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Log.d(TAG, "onJsAlert url: " + url + "; message: " + message);
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        .setNeutralButton("OK", new OnClickListener() {
                            public void onClick(DialogInterface dialog, int arg1) {
                return true;

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment