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
}
}
});
Related
How can I get the certain parts from Website and show in WebView, Android Studio???
Below Image is that I want get part (square in red part)
and site is this : https://kimpga.com
I'd like to display only the red part as an Android web view. What should I do?
and then I used this method: webView.loadUrl("javascript:document.getElementsByClassName('').style.display='none'");
but that website doesn't have div id and doesn't work method!
+) add homepage source
Please understand to attach the overall picture. I'd like to show you the part of div selected in blue through a web view.
I solved it!
using
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView webView, String url)
{
webView.loadUrl("javascript:(function() { " +
"document.getElementsByClassName(//classname//)[index of document].style.display='none'; " +
"})()");
}
});
I wish to show only the login-page element of a URL. My current approach which is not working:
web.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:document.getElementByClassName('login-page')");
}
});
web.loadUrl("https://www.reddit.com/reddits/login");
For mobile screens you should develop separate screens and embed it.
Another way is if your web page supports p/# tag (i.e) like
https://developer.android.com/guide/components/fundamentals.html#Components
https://www.reddit.com/reddits#login this should navigate the page directly to login form
Anyhow I prefer you should develop the page for mobile screens.
Your approach is close to working, but you should modify the existing page instead of trying to load a subset of the existing page as a new one.
The following hides elements with class SectionToRemove, you could instead hide all and only make visible the area you need:
#Override
public void onPageFinished(final WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:$('.SectionToRemove').hide();");
}
That being said, Reddit has extensive APIs that should be used if possible, this approach may breach Terms of Service.
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 using a WebView to display a picture from the net, on occasions the picture will not load.
mPicture.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
setProgressBarIndeterminateVisibility(false);
Log("Content height: " + mPicture.getContentHeight());
}
});
The above code works fine for finding out when the WebView has loaded the content as best it can, but how can i tell if the picture was actually found/loaded. I was hoping to this by using the size of the content loaded, but there is not method for this, tried using the content height, but get the same value for no pic loaded as i do for a portrait loaded picture.
Any idea's?
No, you cannot detect if a an individual element of a the HTML code has been loaded. You can only tell if the page is loaded.
The page has finished loading when all the content has been downloaded or all the content that can be downloaded has been downloaded.
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