问题
i am trying to give Progress Bar till the content of the WebView is loaded.
in xml
<ProgressBar android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="5dp"
android:max="100"
android:visibility="gone" />
<WebView
android:id="@+id/intro2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
in java
ProgressBar pbr= (ProgressBar)findViewById(R.id.progressbar);
pbr.setProgress(0);
pbr.setVisibility(View.VISIBLE);
WebView view = (WebView) findViewById(R.id.mgs1);
String text = "<html><head><style type=\"text/css\">table, th, td { table-layout: fixed; padding: 2px; text-align: center; background-color: #339933; border: 2px solid white; border-collapse: collapse; }\"></style></head> <body style=\"text-align:justify; color:#ffffff; margin-right :30px; background-color:#339933;\"> %s </body></Html>";
String data = "<table style=width: 30%; > <tr><td>17</td> <td>24</td> <td>1</td> <td>8</td> <td>15</td></tr> </table>";
view.loadData(String.format(text, data), "text/html", "utf-8");
view.getSettings().setDefaultFontSize(12);
After this what should i do?
回答1:
Use setWebViewClient
for webview as follows for your purpose. Read the comments for where t start and stop the progress bar.
view.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// Show progressbar
pbr.setVisibility(View.VISIBLE);
}
@Override
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) {
// Show error
// Stop spinner or progressbar
pbr.setVisibility(View.GONE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Stop spinner or progressBar
pbr.setVisibility(View.GONE);
}
});
回答2:
Add following 2 lines in onCreate
String url = "your url";
new LoadSocialNetworkUrlTask().execute(url);
Following is the asynctask
public class LoadSocialNetworkUrlTask extends AsyncTask<String, String, Void> {
protected void onPreExecute() {
dialog = new ProgressDialog(SocialNetworkActivity.this);
dialog.setMessage(getResources().getString(R.string.loading));
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
protected void onProgressUpdate(final String... url) {
try {
((WebView) webView).getSettings().setJavaScriptEnabled(true);
((WebView) webView).setBackgroundColor(Color.TRANSPARENT);
((WebView) webView).setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view,
SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO hide your progress image
super.onPageFinished(view, url);
dialog.dismiss();
}
});
((WebView) webView).loadUrl(url[0]);
} catch (Exception e) {
e.printStackTrace();
dialog.dismiss();
}
}
@Override
protected Void doInBackground(String... url) {
try {
publishProgress(url);
} catch (Exception e) {
e.printStackTrace();
dialog.dismiss();
}
return null;
}
}
回答3:
You can do this
view.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
pbr.setVisibility(ProgressBar.VISIBLE);
pbr.setProgress(progress);
if(progress==100){
pg.setVisibility(ProgressBar.GONE);
}
}
});
来源:https://stackoverflow.com/questions/26988949/how-to-show-the-progress-bar-till-the-webview-content-is-appears-in-android