How to improve webview load time - android

I have a URL which I can load in webview. If I open the webview through browser it takes less time, but if I load the URL in webview it takes more time. Also if I have to reuse the same webview page with the same URL in another screen then I am unable to do that because if I use the same webview object in different screens then also it takes time to load the URL.
How do I make my webview to load a URL instantly?

You could use the YSlow or Google PageLoad plugins for your browser, to get specific tips on how to improve page load and speed up your page.

You could try a different cache mode: http://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode%28int%29
or, if the page doesn't change, just use loadDataWithBaseURL:
http://developer.android.com/reference/android/webkit/WebView.html#loadDataWithBaseURL%28java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String%29

By default, a WebView provides no browser-like widgets, does not
enable JavaScript and web page errors are ignored.
here i paste small part of code of zirco-browser
settings.setJavaScriptEnabled(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_ENABLE_JAVASCRIPT, true));
settings.setLoadsImagesAutomatically(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_ENABLE_IMAGES, true));
settings.setUseWideViewPort(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_USE_WIDE_VIEWPORT, true));
settings.setLoadWithOverviewMode(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_LOAD_WITH_OVERVIEW, false));
settings.setSaveFormData(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_ENABLE_FORM_DATA, true));
settings.setSavePassword(Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_BROWSER_ENABLE_PASSWORDS, true));
settings.setDefaultZoom(ZoomDensity.valueOf(Controller.getInstance().getPreferences().getString(Constants.PREFERENCES_DEFAULT_ZOOM_LEVEL, ZoomDensity.MEDIUM.toString())));
settings.setUserAgentString(Controller.getInstance().getPreferences().getString(Constants.PREFERENCES_BROWSER_USER_AGENT, Constants.USER_AGENT_DEFAULT));

set render priority high for your web view can solve problem up to some extent
its some thing like this
webview.getSettings().setRenderPriority(RenderPriority.HIGH);

Related

Android Webview loading slow for the first time

I have created a library to start an activity with a Webview.
I'm trying to load local HTML file which with an Iframe inside it Whenever I'm starting the activity, for first time it is taking approx. of 5 seconds to display iframe. The 2nd time it is taking approx. of 2 seconds to display.
How can i reduce load time, for the first load?
Here are the two java files respectively.
WebviewOverlay.java
String botUrl = "someurl";
myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
myWebView.loadUrl(botUrl);
return myWebView;
WebviewActivity.java
wb = new WebviewOverlay();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.container, wb).commit();
This one is not anything to do with Android, web page has lot of data and or may be high resolution images which taking time or any scripts on the page which taking time while loading
You can use precaching to speed up the webpage load.
Load data(HTML) from the server and save it to a text file in our device memory.
Start a coroutine to load the webpage and store in local storage
Create a new activity in which we will create a webview which will be used to load this cached webpage.
Change some settings of our webview to enable support for javascript, domstorage, database etc.
Attach webclient to our webview
Load our previously created file which contains all our HTML cached and load it to the webview
Here's a Github repo that does exactly what you want it to do.
https://github.com/iambaljeet/WebViewPreCache
Try using optimized images in your Webpage. Resize it from mspaint

How can i render html with using geckoview?

I wanted to download a html file from a website first and edit that website and print that html with geckoview render engine
I know how to fetch a html and parse it but i don't know how to render it with using the renderengine
Geckoview just uses a url of a website
I don't want to use webview because i don't think it's not that good to make my own web browser app
GeckoSession has loadString(String htmlString, String mimeType) method:
GeckoSession geckoSession = ...;// here you start session in your GeckoView
geckoSession.loadString(yourHTMLString, "text/html");
geckoSession.loadData("My First Heading. My first paragraph.".getBytes(StandardCharsets.UTF_8),"text/html");
geckoSession.loadString("<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>","text/html") displays blank page. Awesome!

is it possible to save/retrieve specific data from webview cache?

i load a web page in a webview that contains a form, i want to know if there is a way to save the data that user put in the form in the webview cache and auto fill it in his next visit?
this is all i have done for now.
webView.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
not sure if it's the right way but, what about running js script on webview to get form input values, navigating DOM tree of html or using selector with id ?
once you got them you can store them in your app and for repopulate, before loading the page in webview, you manipulate html (if you could) or you use another js script to set the values
i'm not sure but i thing cache is only for cachable values (images, css...) not for input

Titanium webview.reload() not working correct

When I try to reload the titanium webview true webview.reload(), the view does not reload correctly. Instead if loading the page it gives me a page not found.
what i'm doing:
In Titanium i make use of webviews to display data. These webviews make use of HTML that is stored in the local filesystem that Titanium offers. The webview is called url is set by :
webview.setUrl( Ti.Filesystem.applicationDataDirectory.toString() + 'index.html');
This sets the proper url for the webview, it let's me see the correct html page. When I use webview.reload(), it seems lost... is there a way to reload the webview, or should i remove and then add the webview again?
Setting a URL for WebView the resource is usually loaded from the Resources folder.
So try to move all HTML files there (into Resources, same folder where app.js is located) and simply use.
webview.setUrl('index.html');
This has worked for me both on iOS and Android.
(There is an issue related to Android regarding WebView and setting its content by html property but this shouldn't matter here)

page not available when opening link in webview

I take the response from an HTTP connection in the form of string and show that to webview like this:
WebView engine = (WebView)findViewById(R.id.webview);
engine.loadData(endResult, "text/html", "UTF-8"); /*endresult is string*/
I actually get a response that contains the google page (google search result direct from google.com).
The loadData method works well i.e it shows the web page but when I click on one of the links on that page it shows "page not available" and said that "xyz link might be temporarily down or it may have moved to permanently to a new web address".
this happens for all links accept the first present link on that page. i.e it shows correct page from first link on that page but fails for others..
I noticed that OSes prior to 2.3 failed to follow links if setHorizontalScrollBarEnabled and setVerticalScrollBarEnabled are set to false.
try to use loadDataWithBaseURL of the WebView class
I would avoid using engine.loadData - it seems to cause all sorts of crazy problems.
Use engine.loadDataWithBaseURL instead, and pass the base URL of where the content exists. I would think that the content you are loading is using relative paths in it's HTML so it's looking inside your app resources. By specifying the base URL you get around this problem.

Categories

Resources