Reputation: 8408
I have an activity containing a WebView
and ProgressBar
. During the loading of a web page, the progress bar appears and gradually horizontally fills. How can the visibility of the progress bar be set to View.GONE
after the animation has finished?
public class WebviewActivity extends AppCompatActivity {
WebView myWebView;
ProgressBar pBar;
ProgressBar myProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_webview);
}
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.fragment_webview);
myProgressBar = findViewById(R.id.mProgressBar);
myProgressBar.setMax(100);
String url = getIntent().getStringExtra("url");
myWebView = findViewById(R.id.webview01);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(url);
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
myProgressBar.setProgress(newProgress);
if (newProgress == 100) {
AlphaAnimation fadeOut;
fadeOut = new AlphaAnimation(1,0);
fadeOut.setDuration(500);
fadeOut.setFillAfter(true);
myProgressBar.startAnimation(fadeOut);
// myProgressBar.setVisibility(View.GONE);
} else {
myProgressBar.setVisibility(View.VISIBLE);
}
}
});
}
private class WebViewClient extends android.webkit.WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
String url=request.getUrl().toString();
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// myProgressBar.setVisibility(View.GONE);
}
}
}
Upvotes: 2
Views: 1368
Reputation: 2111
Try setting an animation listener to the animation object (fadeOut):
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
myProgressBar.setProgress(newProgress);
if (newProgress == 100) {
AlphaAnimation fadeOut;
fadeOut = new AlphaAnimation(1, 0);
fadeOut.setDuration(500);
fadeOut.setFillAfter(true);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// whatever you want to happen when the fadeOut animation starts
}
@Override
public void onAnimationEnd(Animation animation) {
// whatever you want to happen when the fadeOut animation ends
myProgressBar.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
// whatever you want to happen when the fadeOut animation repeats itself
}
});
myProgressBar.startAnimation(fadeOut);
} else {
myProgressBar.setVisibility(View.VISIBLE);
}
}
});
With the callbacks above, you can hook into AnimationStart
, AnimationEnd
and AnimationRepeat
.
Upvotes: 1
Reputation: 17824
Use a listener:
fadeOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimantionEnd(Animation animation) {
myProgressBar.setVisibility(View.GONE);
}
});
Upvotes: 2