I’ve got an Android app that uses a WebView. At the startup of the app it checks if the WebView can connect to the server or not. If it can connect there’s no problem, the app loads instantly and the WebView is showing the page. But if the server is not available or the connection doesn’t work, it takes a few seconds (5-10) before the error message shows. That’s because when you have a slow connection, it doesn’t show the error message continiously.
So with no connection you see a black screen for about 5-10 seconds. What I want is just a simple loading dialog at the startup of the app. So that you don’t see the black screen and people know the app is doing something. How can I archieve this with the most simple but effective way??
Thank you!!
EDIT:
Okay this is what I now have, I think it’s working but is this the right way? And is it possible to use this Async thread also in the shouldOverideUrlLoading? How can I do that?
public class WebviewActivity extends MainActivity {
private WebView myWebView;
private ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
final ScrollView mainScrollView = (ScrollView)findViewById(R.id.ScrollView01);
myWebView = (WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
progressDialog = ProgressDialog.show(WebviewActivity.this, "", "Loading...", true);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressDialog.show();
if ( isOnline() == false || isOnlineHost() == false )
myWebView.loadUrl("file:///android_asset/errorpage.html");
else
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
progressDialog.dismiss();
mainScrollView.fullScroll(ScrollView.FOCUS_UP);
}
});
myWebView.requestFocus(View.FOCUS_DOWN);
myWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
@Override
public void onResume() {
super.onResume();
new checkConnection().execute();
}
//DOING THE CONNECTION CHECK IN ANOTHER THREAD
public class checkConnection extends AsyncTask<String, Integer, String>{
private ProgressDialog progressDialog;
protected void onPreExecute(){
progressDialog = ProgressDialog.show(WebviewActivity.this, "", "Loading...", true);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
if ( isOnline() == true && isOnlineHost() == true )
myWebView.loadUrl(webLink);
else
myWebView.loadUrl("file:///android_asset/errorpage.html");
progressDialog.dismiss();
return null;
}
}
}
Use an
AsyncTaskto check for the connectivity in the background while theProgressDialogis shown. Dismiss the dialog after the check is complete.