I'm trying to load a web in a webview, but when the page is loaded, the webview carry on refreshing the view in a loop. How I can avoid this?
My code to load the page is the next:
In the onCreateView:
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pathto.get_register_service());
webView.setWebViewClient(new RegisterWebViewClient());
webView.invalidate();
The custom class of the WebViewClient:
private class RegisterWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl(url);
progressWheel.setVisibility(View.GONE);
ll_error_message.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
progressWheel.setVisibility(View.GONE);
ll_error_message.setVisibility(View.VISIBLE);
webView.setVisibility(View.GONE);
}
}
You are reloading page in onPageFinished event with view.loadUrl(url);
onPageFinished is triggered when page has finished loading and you should not reload the page inside that event or you will end up in endless loop.
Related
I know how to get notified when it finishes loading a web page, but is there any way to know when it starts loading a new one that is initiated from a link from the original web page?
The reason that i want it is to make a ProgressBar visible whenever a page starts loading, and make it invisible whenever it finishes.
UPDATE: What I'm asking is if it's possible to know when a new page starts loading a page. Although from the link I found onPageStarted, and it works.
Check with the following
webView.setWebViewClient(new WebViewClient() {
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
//Show loader on url load
}
public void onPageFinished(WebView view, String url) {
//cancel loader complted
}
});
// Set Javascript enabled on webview
webView.getSettings().setJavaScriptEnabled(true);
To listen for a web page starting loading you should override onPageStarted, to listen for finishing you should override onPageFinished
and just to make it more complete, listen for error with onReceivedError
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
// runs when a page starts loading
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// page finishes loading
progressBar.setVisibility(View.GONE);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
// runs when there's a failure in loading page
progressBar.setVisibility(View.GONE);
Toast.makeText(context, "Failure on loading web page", Toast.LENGTH_SHORT).show();
}
});
The method ShouldOverrideUrlLoading in my custom webview client is not called when i click on a link inside html loaded in the webview with webView.LoadDataWithBaseURL(null, body, "text/html", "utf-8", null);
So as a result i'm getting a blank page :(
How can i intercept click events or is there any way that shouldoverrideurloading can be called?
class MyWebViewClient : WebViewClient
{
public delegate void PageLoaded();
public event PageLoaded PageLoadedEvent;
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
view.LoadUrl (url);
return true;
}
public override void OnPageStarted (WebView view, string url, Android.Graphics.Bitmap favicon)
{
base.OnPageStarted (view, url, favicon);
}
public override void OnPageFinished (WebView view, string url)
{
if(PageLoadedEvent != null)
{
PageLoadedEvent ();
}
base.OnPageFinished (view, url);
}
public override void OnReceivedError (WebView view, ClientError errorCode, string description, string failingUrl)
{
base.OnReceivedError (view, errorCode, description, failingUrl);
}
}
As explained in Migrating to WebView in Android 4.4 "If you loaded the page by calling loadData() or loadDataWithBaseURL() with an invalid or null base URL, then you will not receive the shouldOverrideUrlLoading() callback for this type of link on the page." You should change your base URL when calling LoadDataWithBaseURL() from null to a valid URL.
I have a webview that must make a LOG when onReceivedError() is called. But the problem is that when i have very bad wifi connection (almost 100% loss) and the webview is showing a Page Not Available html error, the method onPageFinished is being called and onReceivedError is not being called
How can this be solved? I want that when the page is not available the method onReceivedError gets called!
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("WEBVIEw","ON PAGE FINISHED");
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.d("WEBVIEw","ON PAGE error");
}
});
onPageFinished will always be triggered even if there is an error. It would be great to have a method called onPageSucceeded - but this is fairly easy to create.
Create a property on the top of the page private boolean webviewSuccess = true; and
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("WEBVIEW","ON PAGE FINISHED");
if(webviewSuccess) {
Log.d("WEBVIEW","ON PAGE SUCCEEDED");
}
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
webviewSuccess = false;
Log.d("WEBVIEW","ON PAGE error");
}
Is it possible to restrict WebViews so that when you have let's say one webview for website but you just want the view to stick to that page and only that page?
Great example is I have a twitter WebView that is following a tag. I just want the user to be able to follow that tag not be able to go deeper into twitter stuff.
It's already a pre-set link,
Button--> opens web view with set link to twitter.
So has to be some kind of check for on press or something like I think.
something like this should do it.
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("somedomain.com")) {
view.loadUrl(url);
}
return true;
}
});
In-fact the above is a good solution but if you want an elaborate one. Try this one. I don't know whether this is proper solution for your query. play around with this.
webview.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!url.equals("your_domain_name.com/")){
setResult(RESULT_OK);
finish();
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(!url.equals("your_domain_name.com/")){
finish();
}
}
#Override
public void onTooManyRedirects(WebView view, Message cancelMsg,
Message continueMsg) {
super.onTooManyRedirects(view, cancelMsg, continueMsg);
cancelMsg.sendToTarget();
}
}
I want to trigger a function after webview's finished loading a webpage and received no error. According to my experiment, the onPageFinished() will be triggered several times and in most of the cases, it will be executed before onReceivedError().
So how can I know that the webpage is loaded and no error is received?
You can set the WebViewClient in the WebView and Override its Callback methods which will let you know when the webpage has finished loading the page. For example
webView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
if(progess!=null){
progess.dismiss();
}
progess = ProgressDialog.show(LoadUrl.this, "", "Loading please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (Uri.parse(url).getHost().equals("callback")) {
// This is my web site, so do not override; let my WebView load the page
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view, url);
// Add YOUR CODE HERE
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(LoadUrl.this, "Sorry! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
handler.proceed();
}
}
Here onPageFinished(WebView view, String url) will be called when webView has finished loading the current Page.
Hope it helps :)