Android: WebView improve loading speed of local html Files

longi picture longi · Apr 12, 2016 · Viewed 7.5k times · Source

Is there a way to improve the speed of loading an local .html file into a WebView. The .html files are stored in the /assets Folder.

As you can see in the video (Sorry, link is broken!), TextView (red beackground) is rendered before the transistion starts, while the text in the WebView is shown afterwards. How can i achieve to load the WebView as fast as the TextView?

//current implementation
webView.setInitialScale(1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);

webView.loadUrl("file:///android_asset/disclaimer.html");

SUMMARY

It's not possible. I tried all comments here and it didn't make a difference.

The reason I ask was, that we had an application for iOS and Android, which mainly consists of simple TextViews and Images. So we had the idea of creating local html files, which we could use in both applications. In iOS this works like a charm, but in Android we couldn't get rid of the loading time, so I always had a blank screen and after 100-200ms the content appears.

I guess Androids WebView starts rendering if the Activity is visible. This totally make sense in online mode, because you don't want to load several html pages, which the user opens in a new tap in the background, before he actually focus them. However, in offline mode (local html files stored in the application assets) this behaviour is unnecessary, but you cannot change it.

Now we really now why phonegap & co sucks.

Just for the record: In the end, I used an activity with an empty LinearLayout container, where you could insert contents programatically. Each style (Headline 1, Headline 2, Content...) had an own layout xml file

public void insertHeadline(int id){

    String text = getString(id);
    TextView headline = (TextView) inflater.inflate(R.layout.layout_text_headline, null, false);
    headline.setText(text);

    //add this TextView to the empty container
    myLinearLayoutContainerView.addView(headline);

}

Answer

Nirmal Shethwala picture Nirmal Shethwala · Apr 21, 2016

It depends on the web application being loaded. Try some of the approaches below:

Set higher render priority (deprecated from API 18+):

webview.getSettings().setRenderPriority(RenderPriority.HIGH);

Enable/disable hardware acceleration:

if (Build.VERSION.SDK_INT >= 19) {
// chromium, enable hardware acceleration
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
   // older android version, disable hardware acceleration
   webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

Disable the cache:

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