I have WebView in my activity and I need to load some local string ( html ) in that webview. Problem is that local html contains images from remoted servers so it needs time to download and show. Is there way to notify user to wait, like spinning dialog or something ? How to notify user to wait content ?
There is not built-in function for doing what you need.
However, you can set a WebViewClient on WebView (see webView.setWebViewClient()) and override the methods onPageStarted() and onPageFinished(). In this way you can show a ProgressDialog or other progress indicator (use a RotateAnimation for example) when the page has started loading and dismiss it later when the page has finished loading.
webView.setWebViewClient(new WebViewClient(){
public void onPageStarted(WebView view, String url, Bitmap favicon){
//show progress indicator
}
public void onPageFinished(WebView view, String url){
//hide progress indicator
}
});
You could do that in your html page using css or javascript.
You could set a webviewCLient to the webview and you will get callback onPageFinished.
ts working fine on all devices Use this link :- Load Web View ProgressDialog
Related
I have an Activity in my app which has a Webview in it which opens a URL provided to it and is then redirected to the landing page of the website. The issue I'm facing is once the user is on landing page the redirection is same page redirection i.e. if the landing page is
www.example.com/landing
The redirection is
www.example.com/landing/#other_page
Which does not call the
onPageStarted(WebView view, String url, Bitmap favicon)
of the webview, but
onPageFinished(WebView view, String url)
is called.
Is there a way to track when the redirect is starting to load since I need to log the time required to load a page even if the page is same page redirection.
Thanks in advance!
Edit: According to the documentation:
onPageStarted will not be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe, it will also not be called for fragment navigations (navigations to #fragment_id).
Which is exactly my case. Is there any other way to track the start and end of loading of fragment navigation?
I think you are looking for that method
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("Webview", url);
return false;
}
});
How to go EXACTLY to http://example.com/index.html#label if content to webview loading via WebView.loadDataWithBaseURL ?
By default webview go to root only. How go to label ?
WebView doesn't have such functionality from the box.
You can achieve this with the help of javascript, but this is a bit of ugly.
You should load your html page and then add javascript code to it.
After this you can load your webview and call javascript methods from current page.
Check this link for example how that can be done.
WebView jump to anchor using loadDataWithBaseUrl
Example goes here
webContents.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
String id = "895884";
webContents.loadUrl("javascript:scrollAnchor(" + id + ");");
}
});
I am working on one Android App where I am loading local file contents into webview using following syntax.
url = "file://" + mLocalFilePath + java.io.File.separator + CURRENT_FILE;
webView.loadUrl(url);
The issue is the webview loads file contents and also loads the various images from url present in webpage ( webpage has many images ).
The challenge I am facing is I want to get notified when webview finish rendering entire html page.I want to get notified when webpage is 100% loaded.
I reffered various forums and also tried using
window.onload function
as well as
public void onPageFinished(WebView view, String url)
But I did not get desire output,I get callback before html page is completely loaded.I am looking for way so that I will get notified only when webpage has finished rendering all the images present in web page.( i.e Webpage is 100% loaded.)
I am not looking for complete answer even hints will be appreciated.
Thanks
Try this way
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress==100){
// page loading completes
}
}
});
I used deprecated PictureListener that start when all images on the pages are loaded. It deprecated and you need at least one image in the html but if you can manage this (you could place transparent 1px*1px image) it's a good solution.
wv.setPictureListener(new PictureListener() {
#Override
public void onNewPicture(WebView view, Picture picture) {
// some code here
}
}
});
When is shouldOverrideUrlLoading method called?
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
}
});
Is it called during initial loading of url? e.g. webView.loadUrl( "file:///android_asset/html/index.html");
Is it called everytime URL of webview changes?
Any reference? I didn't find one. Thanks
It does however, get called when the WebView to load a different URL from the one the user had requested.
Calling loadUrl() will also trigger the shouldOverrideUrlLoading() method. (Only when a new url is about to be loaded.)
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If
WebViewClient is not provided, by default WebView will ask Activity
Manager to choose the proper handler for the url. If WebViewClient is
provided, return true means the host application handles the url,
while return false means the current WebView handles the url.
Ref : public boolean shouldOverrideUrlLoading (WebView view, String url)
Below is the answer for your both the questions:
As per the document, it will manage every time new URL is about to load in current WebView.
Is there anyway to tell if content retreived from a URL into a WebView is full loaded?
EDIT: I am trying to display a indetermined progress widget until the webview is fully loaded?
How would i implement this with WebView.getprogress()?
You can call WebView.getProgress() and see if it's at 100%.
EDIT: Add this to your WebView:
WebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
As mentioned in this question.
For the indeterminate progress spinner, create a ProgressDialog and show it when you start loading the webpage. Then in the onPageFinished() add the ProgressDialog.dismiss() to close it. Refer to this guide to see how to make progress dialogs.