I was trying to show a web page on my web view. and in case if an error occurs or page loading fails I want to show a sad smiley and a text showing that your page is not loaded
Because onReceivedError
called before onPageFinished
, so your sadSmiley
and errorText
gone.
Try below code,
boolean errorOccurred = false; // Global variable
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
hideError();
showProgress();
Toast.makeText(Test.this, "start loading", Toast.LENGTH_SHORT).show();
errorOccurred=false;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (!errorOccurred) {
hideError();
}
hideProgress();
Toast.makeText(Test.this, "Web view was loaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
errorOccurred = true;
hideProgress();
showError();
Toast.makeText(Test.this, "Could not load your page", Toast.LENGTH_SHORT).show();
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(Test.this, "error", Toast.LENGTH_SHORT).show();
}
});
Move the line super.onReceivedError(view,errorCode,description,failingUrl);
above hideProgress();
in onReceivedError()
method.