Hide the progress bar after loading WebView in Android
•
Android
When I add this code to my code, I want to know what the problem is
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
web.loadUrl("file:///android_asset/error.html");
}});
Textview will never become invisible after the progress bar and WebView are loaded
public class YazdMap extends ActionBarActivity {
WebView web;
ProgressBar progressBar;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yazd_map);
web = (WebView) findViewById(R.id.webview1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
txt = (TextView) findViewById(R.id.txt);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://google.com/");
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
web.loadUrl("file:///android_asset/error.html");
}
});
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(view.GONE);
txt.setVisibility(view.GONE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
{web.goBack();
return true;}
return super.onKeyDown(keyCode, event);
}
}
resolvent:
1.) web.setwebviewclient (New webviewclient() {will overwrite the previously set WebView and the new mywebclient(), so please delete this
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
web.loadUrl("file:///android_asset/error.html");
}
});
2.) move your onreceivederror to the mywebclient class so that the code
public class YazdMap extends ActionBarActivity {
WebView web;
ProgressBar progressBar;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yazd_map);
web = (WebView) findViewById(R.id.webview1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
txt = (TextView) findViewById(R.id.txt);
web.setWebViewClient(new MyWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://google.com/");
}
public class MyWebClient extends WebViewClient
{
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(view.GONE);
txt.setVisibility(view.GONE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
view.loadUrl("file:///android_asset/error.html");
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
{web.goBack();
return true;}
return super.onKeyDown(keyCode, event);
}
}
Note: I changed the name from mywebclient to mywebclient because by convention, the class must start with an uppercase letter
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码