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.
Related
I faced a problem, and hope somebody would be kind and give me a hand with it.
The problem is: I've got a webView in my application to open links on some news within my app (and I don't want to open them with browser or anything like that). And this news sometimes contains images.
When user tap on those images, they opens in the same size area as main news. And it looks quite bad.
[in picture bottom you can see top of the opened image][1]
[1]: https://i.stack.imgur.com/6M4fg.jpg
I already have a fragment in my structure to open images by url, but I don't know how to handle this "on-image" click in webView and grab image url from it to go to my ImageView fragment.
I read a bit about using javascript to take id from page html code, but there are no unique id's in html for those pictures.
You can provide your own WebViewClient and override shouldOverrideUrlLoading and then intercept click on a image.
Some example how it can looks like:
webView.webViewClient = object: WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
return if (isImage(request)) {
openImageViewFragment(request.url)
false // indicates that webview shouldn't do anything with the request
} else {
super.shouldOverrideUrlLoading(view, request)
}
}
}
I am trying to find out if the content in my webview is smaller than the size of the webview itself.(essentially no vertical scrollbar)
on looking around I found (webview.getContentHeight()*getResources().getDisplayMetrics().density )< webviewheight.getMeasuredHeight())
But I keep getting 0 for both when I try and log it.
#Override
public void onPageFinished(WebView webview, String url) {
super.onPageFinished(webview, url);
Log.i("scrolltest page finished",":"+ mArticleView.isVerticalScrollBarEnabled());
Log.i("heighttest page finished", ":"+( mArticleView.getContentHeight()*getResources().getDisplayMetrics().density )+"<"+ mArticleView.getHeight());
}
I always get a true for isVerticalScrollBarEnabled and keep getting Zeros for my heights, what am I doing wrong? is there a simpler way to find out if the content of my webview is smaller than its height?
NOTE:- mArticleView here is a webview.
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
}
}
});
There is a rather specific webpage loaded into WebView which URL is like http://www.site.com/mob/ (basically a mobile-optimized web page). This webpage display 25 articles only and on the bottom is a button "More articles".
When a user presses it, I catch URL http://www.site.com/Web/MobHomeItems.aspx?page=N (where N is 2, 3, 4...) and after that another 25 items have been loaded on the same screen.
Now, when I click on some article and go to article details, and later return to the page via the Back key, the WebView forgets how many articles have been loaded and simply loads the default page with 25 displayed articles. Imagine how frustrating this would be to a user if he came to 100th article.
I tried overriding many methods in WebClient and in WebChromeClient, but so far I have been unable to load N number of pages loaded via "More Articles" button. For example, I first thought this would help, but it did not.
#Override
public void onLoadResource(WebView view, String url) {
//http://www.site.com/Web/MobHomeItems.aspx?page=2
if (url.contains("?page=")) {
//save this URL for later and on return from
// article details, pass it to LoadResource()
super.onLoadResource(view, url);
}
Then I tried similar approach with other method - basically remembering how many pages have been loaded on the main page, and then on return from article details, simply tell webview to load this URL.
Can anyone help me? How to append loaded pages to the main page? Should I use JavaScript here maybe?
PS. Loading mentioned URL http://www.site.com/Web/MobHomeItems.aspx?page=N does not help as it loads this concrete page into the WebView only, and it does not append this Nth page to the main page.
EDIT
As #Raghunandan asked, I do not have problems loading back to 1st page (?page=1). This is default when user presses Back button on article details. I want to load to the page where a user was before pressing article details. If he was on ?page=100, I want to load back to that page e.g. I want to have 25x100 articles open. Again, default is always "open 25 articles or ?page=1 or http://www.site.com".
Override the method shouldOverrideUrlLoading of WebViewClient.
like this:
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url is kind of article detail) {
WebView newOne = new WebView(); // create a new Webview for displaying the details.
view.setVisibility(View.INVISIBLE); // hiding current page (article list)
return true; // To tell the WebView we have process this url.
}
return false;
}
The user click one link of article's detail.
shouldOverriderUrlLoading would be triggered.
We created one new WebView to open the url.
Hiding current page
The user reading artical
The user click back key, close the newOne WebView then make the
previous WebView visible.The article list will show up immediately and remained the old statement
.
There is a another way to do this.
The WebSettings has a private method "setPageCacheCapacity" , the default value is 0 , you could enlarge it (may be 5).
You can access this method by using reflection of java.
The method can enable WebView to cache more than one document. In the other word. when user press the back key, the WebView will go back to the older document.
I have an app with a previously-existing, web-based registration process that I am trying to use inside a WebView. I need to add some style tags to the html in order to hide some elements for better displaying the content inside my app. I can get it to work on initial load, but I cannot figure out how to do it from one page to the next inside the WebView. Here is what I have working:
On initial load of the site, I am getting the raw html and appending "<style>MY STYLES HERE</style>" to the string before calling
wv.loadDataWithBaseURL(url, rawHtml, null, "UTF-8", url);
This works perfectly, but if a user clicks a link on the page and it loads another page into the WebView, then this code does not get called and the style tag is lost.
I assume I need to override "shouldOverrideUrlLoading" in the WebViewClient, but I don't know how to intercept the html from here. I thought I would try something like:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String rawHtml = getRawHtml(url) + "<style>...</style>";
wv.loadDataWithBaseURL(url, rawHtml, null, "UTF-8", url);
}
But this obviously sends it into an endless loop of intercepting the load to start a new load.
I have also tried overriding onPageFinished and doing:
wv.loadUrl("javascript:(function() { ... })()");
which works, except that it waits until the entire page is loaded before executing. This causes the page to appear loaded with all of the UI elements in tact, and then all of the ones I am trying to hide suddenly disappear. My ultimate goal is to enhance the look and feel of the site on a mobile device, so this is not an option.
Is there something else I can do in "shouldOverrideUrlLoading" to inject style tags? Or if not, what else can I try?
I've run into this problem, and depending on the number of redirects, etc, we have not been able to make the injected JavaScript available all the time.
At minimum, you should use the wv.loadUrl("javascript:(function() { ... })()"); approach, but call it in both onPageStarted() and onPageFinished().
Depending on the complexity of your pages, you might need to inject the JavaScript in onLoadResource() as well.