Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: WebView improve loading speed of local html Files

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);

}
like image 328
longi Avatar asked Apr 12 '16 14:04

longi


2 Answers

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);
like image 126
Nirmal Shethwala Avatar answered Nov 08 '22 18:11

Nirmal Shethwala


WebView will always have longer render times than a native TextView. I think it's nature of platform but maybe you can try reading html content to a String before loading screen (for avoid webview from file system operation). And then set String with:

webview.loadDataWithBaseURL("", earlyReadedHtmlString, "text/html", "UTF-8", "");

In theoretically, this must be faster and fair for webview. Another way is waiting for onPageFinished() and then show textview. It can be a workaround.

like image 41
asozcan Avatar answered Nov 08 '22 18:11

asozcan