Is there a way to set the timeout value in WebView? I want the WebView to be timeouted if the url is too slow to response.
Asked
Active
Viewed 2.8k times
17
-
1this shud help http://stackoverflow.com/a/7772884/169630 – suraj jain Apr 12 '12 at 06:02
3 Answers
11
You can do it by setting up a Timer which checks for progress of current page by calling getProgress() and if it is less than some threshold after some specified time then you can dismiss the loading of the current page.

bhups
- 14,345
- 8
- 49
- 57
-
1So do you mean i add timer and check the progress of current page by calling getProgress(), if it's not 100 yet i can call stopLoading(). – user430926 Nov 29 '10 at 11:04
5
We can use onLoadResource
method of WebViewClient
instead of Timer
. Like this:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog.show();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("WEBCLIENT", "onPageFinished");
}
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
Log.d("WEBCLIENT","onLoadResource");
if(webView.getProgress() == 100) {
progressDialog.dismiss();
}
}
}

Melquiades
- 8,496
- 1
- 31
- 46

John
- 8,846
- 8
- 50
- 85
-
I don't think this would work if the web view is completely stuck. – Przemysław Wrzesiński Sep 21 '16 at 18:34
-
1If the webpage is stuck means, its progress will not be 100 , So the progress bar will be displayed until , the webView.getProgress() is 100 – John Dec 12 '16 at 13:12
-
3The question was not about progress bar, but timeout. This method would not work if the webpage loading gets completely stuck at some percent because none of these functions would get called, so there would be no possibility to take action. – Przemysław Wrzesiński Dec 12 '16 at 13:16
-
1yes you are right. Then a timer should be running untill the resources are loaded – John Dec 12 '16 at 13:18
-
1
I use
@Override
public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
final Dialog dialog = new Dialog(MainActivity.this, android.R.style.Theme_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.alert_dialog);
Button btTryAgain = dialog.findViewById(R.id.bt_try_again);
btTryAgain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
recreate();
}
});
dialog.show();
//Toast with error conection
Toast.makeText(getApplicationContext(), "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
}
Where -alert_dialog- is a layout with a button to retry

JoaquinBu
- 321
- 2
- 5
-
inside your code, I see use of recreate(); on retry click. So, instead of recreate(); you may use WebView's reload() method. it will attempt to reload the entire web page. – Dhaval Shah Jun 02 '22 at 10:34