Jakub Anioła
Jakub Anioła

Reputation: 330

Javascript on Android WebView not working

I have some issues with Android WebView and Javascript. Some of customers of app said that WebView on app is not showing anything. As I checked - its probably not showing javascript at all (whole webpage is loaded in javascript by react).

That my code:

    public void setupWebView(WebView accessWebView) {
    accessWebView.setWebViewClient(new WebViewClient() {

        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            handleRedirect(accessWebView);
            return true;
        }

    });
    accessWebView.getSettings().setJavaScriptEnabled(true);
    accessWebView.getSettings().setDomStorageEnabled(true);
    accessWebView.loadUrl(URL);

(I have to use WebViewClient, not WebChromeClient, because of the redirect handling)

Is there anything possible to change so the javascript will load on EVERY device with Android +5.0? Is it possible that updating WebView on device will help some users?

Upvotes: 2

Views: 3383

Answers (1)

Łukasz Kobyliński
Łukasz Kobyliński

Reputation: 1111

You need to use setWebChromeClient to enable javascript in your WebView. But don't worry, you can use both setWebChromeClient and setWebViewClient in the same time. Just like in official docs:

  // Let's display the progress in the activity title bar, like the
  // browser app does.
  getWindow().requestFeature(Window.FEATURE_PROGRESS);

  webview.getSettings().setJavaScriptEnabled(true);

  final Activity activity = this;
  webview.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
      // Activities and WebViews measure progress with different      scales.
      // The progress meter will automatically disappear when we reach 100%
      activity.setProgress(progress * 1000);
    }
  });
  webview.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String      description, String failingUrl) {
      Toast.makeText(activity, "Oh no! " + description,      Toast.LENGTH_SHORT).show();
    }
  });

  webview.loadUrl("https://developer.android.com/");

https://developer.android.com/reference/android/webkit/WebView.html

Upvotes: 1

Related Questions