I am currently working on android webview applications. I was wondering if there is a way to load selected pages only in android webview, like just tell the app to load only specific pages,not whole website. Is it possible?
Depends on what you mean by specific pages. You can lock the webview into only a set of URLs by overriding shouldOverrideUrlLoading to return true and just not load any URLs you don't want to. If you mean you want to not load part of a URL I don't think there's a way to do that.
Related
Is there a way for me to edit the contents of the Webview or to crop out some parts of the content, i'm focusing on UI and would like to clean up the content for a project i am working on.
Also lets say I want to display content of the webView individually as well (multiple webviews within an activity) for aesthetic purposes, how would I go about doing that?
One technique I've used in the past is to request the page with an HttpURLConnection rather than the WebView. Then I parse and edit the HTML data and display it in the WebView using the loadData method. You could even use something like JSoup to parse the HTML. However, if you are depending on having the JavaScript run on the page, you may have a problem because using loadData will disrupt the same-origin policy.
In my application am showing contents with URL www.domain_name.com/content?id=5513 . Each content have different ids. When phone have data then it loads WebView from network, otherwise it loads from cache. My issue is that webview loads partially for every URL.
It was backend issue, where pages are loading dynamically to single page. Changed dynamic page to single page.
I am working on hybrid app and wanted to know the best practices or good way of architecting/structuring the application.
Should one use one webview per activity or should have one single activity with one just webview and change the html pages that are being loaded?
Can one use one webview on multiple activities?
Certain web pages requires loading of heavy resources such as videoview. Due to this I was thinking of using single webview with different activities if its feasible.
You should go with Single Activity with Single Webview. Wherein load the static resources (Html files , js files images etc. )from local location and go to server for dynamic data.
Call for data from server better to use native layer to avoid cross scripting issue. Having said that , Write all the API's in Java and call each api from WebView either using JavaScriptInterface Mechanism or using ShouldOverrideURL method of webview. Recommended is JavaScriptInterface.
To your answer for question : Its always feasible having one webview with multiple activities. but that's not recommended. One Activity is more than enough to take care of loading complete website with different pages in webview.
I want to pre load a web page in Android. The web page contains both text and graphical elements. The web page will be displayed in the future in an Activity which has not been created yet.
As far I understand it a WebView for example has to be tied to an Activity, thus it's not possible to use a WebView for this task.
Anyone got any suggestions which does not involve parsing the html page and downloading all the elements "manually"?
According to android's documenation: For obvious security reasons, your application has its own cache, cookie store etc.—it does not share the Browser application's data.
So we can make use of the above info in a smart way by doing this:
Whenever you want to start loading your website, create a new WebView object and request the url from it. the code will look something like this:
WebView view = new WebView(context);
view.loadUrl(myBigWebSite);
Android will cache that webview data and when you request loadUrl from another activity it should be already loaded.
// in your WebView activity
myWebView.loadUrl(myBigWebSite);
For more about webViews visit the documentations page : WebView doc
You can download all required files (images, etc) into some folder on a network thread and then use something like
webview.loadUrl("file:///my_cached_folder/index.html");
Images will also be picked from this folder, if referenced and present. Read about opening local files on webview here.
Is there any way to make a call to the Android web browser, that ships with every phone, to render a web page and return it to my program as an image?
This would be hidden from the user. It would not command the Android browser to open itself up on the user's screen and display that web page. Instead, it would be feeding my program rendered web pages as images, and the user would only see my program and not the Android browser.
Thank you.
You don't need to use the system browser, the HTML rendering capabilities are available by the WebView class. It also has a method WebView.capturePicture(), which you can use to obtain an image of the whole rendered website.
However, it could be tricky to drive the WebView into rendering the web page while it is not visible. It must be attached as some view to the current Activity to start loading pages and render them, however its maybe possible to have it hidden in the background when attaching more than one view to a container view like ViewGroup.