Detect when a WebView loads all its images - android

I am using a WebView that loads a page with some images in it..
I need to detect exactly when it finishes loading and rendering all images in the page.
I tried to use "onPageFinished" but it gets called before all images are completely downloaded and visible.
Same thing goes for "onProgressChanged"..
Is there a method where I can know when exactly does my WebView completely load all its views and images and they are visible on the screen??

In my working code I am using next variant:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(true);
// try to use this pieсe of code
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// put here your code
System.out.println("All images are downloaded and displayed");
}
});
webView.addJavascriptInterface(this, "android");
webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);

if you have several redirects it may fail. This approach solves most problems though
boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(
WebView view, WebResourceRequest request) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
//HIDE LOADING IT HAS FINISHED
} else {
redirect = false;
}
}
});

Related

Webview finishes loading before ALL contents are loaded

Recently I faced a pretty annoying issue, So I am loading an iframe (youtube video) in my webview and checking if the WebView has finished loading, it works for other contents but not for iFrame, it shows the page loading finished way before the iFrame is fully loaded. I tried in both SetWebChromeClient and SetWebViewClient - In both cases, loading finishes way before the iFrame finishes loading.
I have simple WebView
webView.LoadDataWithBaseURL("https://.", IFRAME_HERE, "text/html", "UTF-8", null);
So Setting webView.SetWebViewClient(new WebViewClientClass()); with the following
public override void OnPageStarted(WebView view, string url, Bitmap favicon)
{
base.OnPageStarted(view, url, favicon);
Log.Info("101029", "STARTED");
}
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
Log.Info("101029", "FINISHED");
}
Or setting webView.SetWebChromeClient(new NewClient(ParentLayout, ContentLayout)) with the following
public override void OnProgressChanged(WebView view, int newProgress)
{
base.OnProgressChanged(view, newProgress);
if (newProgress == 100)
{
Log.Info("101029", "Finished");
}
else
{
Log.Info("101029", newProgress.ToString());
}
}
Same things happen, Webview shows loading is finished while the iFrame is still loading. How do I tackle the loading of iFrames to accurately get when the iFrame has finished loading?
any idea? Cheers
EDIT:
I also tried the following for `` (Still no luck)
internal class WebViewClientClass : WebViewClient
{
bool loadingFinished = true;
bool redirect = false;
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
if (!loadingFinished)
{
redirect = true;
}
loadingFinished = false;
Android.Net.Uri url = request.Url;
view.LoadUrl(url.ToString());
return true;
}
public override void OnPageStarted(WebView view, string url, Bitmap favicon)
{
base.OnPageStarted(view, url, favicon);
loadingFinished = false;
Log.Info("101029", "loading started");
}
public override void OnPageFinished(WebView view, string url)
{
if (!redirect)
{
loadingFinished = true;
}
if (loadingFinished && !redirect)
{
base.OnPageFinished(view, url);
Log.Info("101029", "loading finised");
}
else
{
redirect = false;
}
}
}

How to detect when redirects in WebView finish?

I have gone through every related post I found here and still can't get it to work.
I'm opening my WebView with a URL that has redirects (sometimes many redirects) and I want to know when the loading is really finished (URL finished loading with no more redirects).
So any suggestions?
Every post I read said to override shouldOverrideUrlLoading but no matter what I do shouldOverrideUrlLoading does not get called.
My code:
mWebView = (WebView) findViewById(R.id.webview);
WebSettings settings = mWebView.getSettings();
settings.setLoadWithOverviewMode(true);
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new MyWebViewClient());
mUrl = "http://cdn.something.com/something.html";
// mUrl = "https://www.google.com";
mWebView.loadUrl(mUrl);
public class MyWebViewClient extends WebViewClient {
public MyWebViewClient() {}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(TAG, "WebViewClient: shouldOverrideUrlLoading");
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d(TAG, "WebViewClient: onPageStarted: url: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "WebViewClient: onPageFinished: url: " + url);
super.onPageFinished(view, url);
}
}
Thank you
you are overriding the new shouldOverrideUrlLoading method which was added in android N,
you have to override both methods to make it work (old one which is deprecated and new one)
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return handleUri(uri);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return handleUri(uri);
}
refer this
,and onPageFinished will be called when page has finished loading
In my case WebViewClient wasn't showing if there was changes on the webview, I supposed that is something about the web that is been running.
I could get that information from the WebChromeClient with OnProgressChanged, I don't know if this would help people anyway, but here is the code:
webview.webChromeClient = object : WebChromeClient(){
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
if (newProgress == 100) {
Log.d("testing", webview.getOriginalUrl())
Log.d("testing", webview.url)
}
}
}
This way I could know what is been loaded and when is finished.

Android - Jelly Bean - YouTube play video only once

I am using a WebView in order to play YouTube vidoes in my Android app.
It worked fine, but I seem to have a problem with Jelly Bean (and maybe later versions, I don't know yet).
The first time I play a video it works fine, but when I try to play it again or even another video, after closing the WebView and re-open it, I see the video loading but then enters into pause mode (as can be seen in the image attched).
When closing the WebView I'm using this code, which I think is the cause of the problem:
Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webview, (Object[]) null);
Did someone encounter this problem?
Left image - the video is played normally.
Middle image - trying to load it again.
Right image - video is paused and never started.
This is the code I'm using:
private void loadUrl()
{
progress.setVisibility(View.VISIBLE);
webview.setWebChromeClient(new WebChromeClient());
if (Build.VERSION.SDK_INT < 8) {
webview.getSettings().setPluginsEnabled(true);
} else {
webview.getSettings().setPluginState(PluginState.ON);
}
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView wView, String url)
{
progress.setVisibility(View.VISIBLE);
webview.loadUrl(url);
isLoading = true;
return true;
}
#Override
public void onPageStarted (WebView view, String url, android.graphics.Bitmap favicon)
{
progress.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError (WebView view, int errorCode, String description, String failingUrl)
{
progress.setVisibility(View.GONE);
isLoading = false;
}
#Override
public void onPageFinished (WebView view, String url)
{
progress.setVisibility(View.GONE);
isLoading = false;
}
});
webview.loadUrl(url);
}
I solved the problem by clearing/destroying the webview:
if (webview != null)
{
if (isLoading)
{
webview.stopLoading();
}
webview.clearCache(true);
webview.clearView();
webview.freeMemory();
webview.destroy();
}

Clicklistner in webview

The question can be a little vague, I have created a webview and showing the webpage of the URL which I get from some server. Now When i navigate to different pages , On one page there is only a button which will send me a different URL , I need that when user is on that page and click on that button the webview should hide and user continue other operation. I have read that addJavascriptInterface is needed to do this , can anyone tell me if this is possible and how to achieve this , any good tutorial will work.
I also tried this but of no use
wv.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
HitTestResult result = wv.getHitTestResult();
if (result.getType() == HitTestResult.UNKNOWN_TYPE) {
Message msg = mHandler.obtainMessage();
wv.requestFocusNodeHref(msg);
}
return false;
}
});
topupWV.setWebViewClient(new WebViewClient());
topupWV.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("XXXXX")) {
try {
URL urls = new URL(url);
query = urls.getQuery();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
}
you can do something like this for detecting click within a webview
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (isNetworkConnected()) {
//DO YOU OPERATION HERE
}
return true;
}
});

Avoid webview redirect

I have a WebView in my project and its loaded with some initial content. Now when the user clicks on the hyperlink on the WebView it should not reload the WebView, instead it should display the initial content. I tried the below code but it is not working.
detailsAndTerms.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
Please help to solve this problem. Thanks in advance.
detailsAndTerms.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted (WebView view, String url, Bitmap favicon)
{
if (url != termsurl) {
// stop loading page if its not the originalurl.
detailsAndTerms.stopLoading();
}
}
});
Update
Try returning false from the shouldOverrideUrlLoading method.
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return true;
}

Categories

Resources