Reputation: 187
How can I use ProgressDialog to wait while WebView is loading?
Sometimes myText has a huge text and some old devices can't load WebView fast. I want to show ProgressDialog while WebView is loading and when it's already shown I want to dismiss the ProgressDialog.
I have:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Please wait...");
dialog.setIndeterminate(true) ;
dialog.show();
String myText = "some text";
WebView myWebView = (WebView)findViewById(R.id.webView);
myWebView.loadDataWithBaseURL("", myText, "text/html", "UTF-8", "");
dialog.dismiss();
It doesn't show any ProgressDialog.
If I don't use dismiss, I always see my ProgressDialog.
Upvotes: 0
Views: 304
Reputation: 1223
just try this,
webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
progDailog.show();
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, final String url)
{
progDailog.dismiss();
}
});
Upvotes: 0
Reputation: 1720
Put this code in your current Java class which contains web view..
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(dialog.isShowing() && dialog != null) {
dialog.dismiss();
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!dialog.isShowing() && dialog != null) {
dialog.show();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}
And set this
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
below you initialized your webview
Upvotes: 0
Reputation: 3881
Use AsyncTask
.
// ..onCreate code come here...
Button button = (Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
new LongOperation().execute(); // here calling LongOperation class to do `AsyncTask`.
}
});
private class LongOperation extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.class);
dialog.setTitle("Please wait...");
dialog.setIndeterminate(true) ;
dialog.show();
}
@Override
protected String doInBackground(Void... params) {
// background operation comes here..
}
@Override
protected void onPostExecute(String result) {
// close progressDialog here by calling
dialog.dismiss();
}
}
Upvotes: 1