0

I have the following class.

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

public class MainActivity extends Activity {
  private WebView w;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    w = (WebView) findViewById(R.id.activity_main_webview);
    w.getSettings().setJavaScriptEnabled(true);
    w.getSettings().setBuiltInZoomControls(true);
    w.getSettings().setDisplayZoomControls(false);
    w.setClickable(true);
    w.loadUrl("https://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
  }
  @Override
  public void onBackPressed() {
    if(w.canGoBack())w.goBack();
    else super.onBackPressed();
  }
}

If you try clicking on the Try it alert box nothing fires.

How do I fix the WebView?

Lime
  • 13,400
  • 11
  • 56
  • 88

3 Answers3

1

Just set the default WebChromeClient :

w.setWebChromeClient(new WebChromeClient());
Nika Kurdadze
  • 2,502
  • 4
  • 18
  • 28
0

The webview does not show an AlertDialog automatically. You have to use setWebChromeClient to set a WebChromeClient and then override onJsAlert to show an AlertDialog.

hoomi
  • 1,882
  • 2
  • 16
  • 17
0

Similar question asked here WebView blocking pop up windows? Try adding these:

    webView.getSettings().setPluginsEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
Community
  • 1
  • 1