Search code examples
androidwebview

Android WebView Not Loading Correctly


I'm having a few issues regarding the WebViewClient on Android. The site works perfectly on any mobile browser. Including the ChromeViewClient that I have set for debugging purposes.

And the website that I am loading does not have any issues or errors when using any other mobile browser. Using Chrome's inspector and selecting a device, using as mentioned the native Android browser and also tested on an iOS WebView Component to make sure.

The WebViewClient renders "parts" of the website. Images on one page and not the other, buttons that can not be clicked, a slider that does not work, etc. The website that I am loading is very JavaScript and HTML5 intensive. I am completely out of ideas of how to debug this issue further, are there certain JavaScript libraries that the WebViewClient can't load properly? Is there any other method you would recommend I implement while trying to debug this issue? Or am I missing some really small thing that will make me hit my head against the table?

These are the JS files we are using on the website:

  • bootstrap.min.js;
  • jquery.min.js;
  • swiper.jquery.min.js;
  • slideout.min.js;
  • owl.carousel.min.js.

Code for the WebView:

this.webview = (WebView)findViewById(R.id.webView);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);

webview.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        view.loadUrl(url);
        firstLoad = true;
        return true;
    }

    // when the page is finished loading
    public void onPageFinished(WebView view, String url){}

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
        Toast.makeText(getBaseContext(), "Could Not load. " + description, Toast.LENGTH_SHORT).show();

        alertDialog.setTitle("Error");
        alertDialog.setMessage(description);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                return;
            }
        });

        alertDialog.show();
    }
});

webview.loadUrl("mywebsite.com");

Solution

  • I got it working by setDomStorageEnabled(true);

    You need to set this when using local storage.