Android
Android

Reputation: 279

Android. WebView displaying white page

Why the web view can show a white page, although the page opens in the browser. At first there was an error:

net::ERR_CACHE_MISS

but after I added this code:

 wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

there are no errors, but the page does not load, just a white page is displayed. The page opens by clicking on the button inside in webview.Clicking is not processed on the mobile application side.

My manifest also has permission:

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

Please help me. I don’t understand why this can happen. The page also opens successfully on iOS. Here is my code:

private void initView(String url) {
    wvDetail.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("webview://closeScreen")) {
                activity.finish();
                return true;
            } else
                return super.shouldOverrideUrlLoading(view, url);
        }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        Toast.makeText(activity, description, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }
});
wvDetail.getSettings().setJavaScriptEnabled(true);
wvDetail.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wvDetail.loadUrl(url);

}

Upvotes: 1

Views: 1169

Answers (1)

Mateo Hervas
Mateo Hervas

Reputation: 585

It seems to be a problem with the settings of your webview. Try and add the following settings:

WebSettings settings = wvDetail.getSettings();
//if your page needs javascript
settings.setJavaScriptEnabled(true);
//to handle your cache
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setAppCacheEnabled(true);
settings.setAppCachePath(cacheDir.path);

To enable some functionalities of JS you should also use setdomStorageEnabled (see this question):

settings.setDomStorageEnabled(true)

If your page is still not loading and you fully trust the web page that you will load, I would also use this settings to give even more access to the webview:

    settings.setBlockNetworkImage(false);
    settings.setLoadsImagesAutomatically(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        settings.setSafeBrowsingEnabled(false);
    }

    settings.setJavaScriptCanOpenWindowsAutomatically(true);

    settings.setAllowFileAccess(true);

To load the webview with the settings of the meta tag, use:

setUseWideViewPort(true);

Finally you could check upon your webView size and make it fit on your system window like this:

wvDetail.setFitsSystemWindows(true);

Just like a bonus if you are targetting above API level 11, you could set your layer type like this:

wvDetail.setLayerType(View.LAYER_TYPE_HARDWARE, null);

I hope this explanation of the common settings used in a webview makes sense! Depending on your webpage I think that some of them can be useful to you! let me know if it works! :)

Upvotes: 1

Related Questions