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;
}
}
}
Related
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;
}
}
});
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.
This is the code to my WebView. It works great on every device I have tested so far, even a Samsung S4. However, a certain Samsung S4, which the client insists it doesn't have a modified software, displays a white screen every time the app is resumed from background. Any ideas?
private void setupWebView() {
// hide the no connection imageview
// show the webview
noConnectionImageView.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedSslError(WebView view,
SslErrorHandler handler, SslError error) {
handler.proceed();
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
showNoConnectionImage();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// page has started loading
if (mustShowSplashScreen) {
showSplashScreen();
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// page has finished loading
if (mustShowSplashScreen) {
hideSplashScreen();
mustShowSplashScreen = false;
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// check for Internet connection
if (ConnectionDetector.isConnected(Main.this)) {
// check if link is external
if (isExternalUrl(url) {
// external link
new OpenLink(Main.this, url);
} else {
// internal link
view.loadUrl(url);
}
} else {
showNoConnectionImage();
Main.this.url = url;
}
return true;
}
});
}
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;
}
});
I want to have a busy indicator of some sort when pages are loading. My indicator will be full-screen like a splash screen. This is because the android webview blanks the page to white before it starts loading, unlike a normal browser.
A simple solution might be suggested. for example, set loading true in onPageStarted() and set false in onPageFinished().
#Override
public void onPageFinished(WebView view, String url) {
setLoading(false);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setLoading(true);
}
the problem with this is that the website i'm loading results in multiple start / finished cycles for a single click. maybe this is redirects, or iframes. i'm not sure ... but the result is that the "busy" indicator flashes on and off disturbingly.
Considering that, another ideas is to keep a count of pages loading- incrementing in onPageStarted() and decrementing in onPageFinished(). something like this,
private int loadingCount = 0;
#Override
public void onPageFinished(WebView view, String url) {
loadingCount--;
if (loadingCount == 0) {
setLoading(false);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (loadingCount == 0) {
setLoading(true);
}
loadingCount++;
}
I'm running into this problem: page started / page finished are not matched. I'm not sure why exactly, but I think it might have to do w/ redirects.
Any ideas?
Try this:
private boolean loadingFinished = true, redirect = false;
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.setVisibility(View.VISIBLE);
view.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setTitle(url);
loadingFinished = false;
progressDialog = new ProgressDialog(FacebookWebView.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(" Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
if (progressDialog.isShowing()){
progressDialog.dismiss();
}
//finish();
} else {
redirect = false;
}
}
I'm not exactly sure, but this should solve your problem with redirects. In the case of several frames on the same page, probably counting in onPageFinished till the number of frames, and then dismiss "busy dialog", should be a solution.