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);
}
}
}
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);
}
});
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.
Related
i have back button in my webview and its linked with loading screen (when i click back button its show loading animation (its named progressBar in the code).
But i have problem when i make fast few clicks my loading screen is looping.
Its any way to make the back button work only one time on page?
Like 1 click = 1 action, no more and stack.
For example i click on the back button, and its off for next click, but when page reloaded button work again.
Button przycisk_powrot = (Button) findViewById(R.id.przycisk_powrot);
przycisk_powrot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mywebView.goBack();
progressBar.setVisibility(View.VISIBLE);
}
});
Button przycisk_powrot = (Button) findViewById(R.id.przycisk_powrot);
przycisk_powrot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mywebView.goBack();
progressBar.setVisibility(View.VISIBLE);
}
});
#Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
public class WebViewClient extends android.webkit.WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.GONE);
}
}, 1000);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
i have this code here. I want that the Imageview do the animation 5 times. How i need to set the setRepeatCount? How to inilize it?
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = ((ImageView) findViewById(R.id.imageView));
iv.setRotationY(0f);
//iv.animate().setDuration(10);
iv.animate().rotationY(90f).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
iv.animate().rotationY(360f).setListener(null);
}
#Override
public void onAnimationCancel(Animator animation) {
}
});
The method setRepeatCount belongs to Animation class, you are using Animator.
Try this:
int times = 5;
private void flipCoin() {
final Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final ImageView iv = findViewById(R.id.imageView);
iv.setRotationY(0f);
//iv.animate().setDuration(10);
final ViewPropertyAnimator viewPropertyAnimator = iv.animate();
viewPropertyAnimator.rotationY(90f);
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
times--;
if (times > 0) {
iv.setImageDrawable(drawable);
iv.setRotationY(270f);
viewPropertyAnimator.rotationY(360f);
viewPropertyAnimator.start(); //Restart
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
});
viewPropertyAnimator.start(); //Init
}
I'm new to Java and was trying to create a web-view app. The loading widget was created using Progress Dialog but it wouldn't stop. I don't know what is wrong. Please help.
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
mywebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
mywebView.setWebViewClient(new WvClient());
}
private class WvClient extends WebViewClient
{
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
}
#Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
You have declared setWebViewClient twice , ie after load url you again
declared the setWebViewClient with your custom client , correct answer is
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
}
#Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
Place a breakpoint in
onPageFinished
and see if the listener actually get called?
I have a webview that opens up a webpage that contains a some links. However, if I click on each link, it will show "404 Not Found" error. I have done the shouldOverrideUrlLoading inside WebViewClient, but it results the same.
Please help. Thanks in advance.
this is how I set my webview:
WebView web_view = (WebView)view.findViewById(R.id.web_view);
WebSettings webSettings = web_view.getSettings();
web_view.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
super.onProgressChanged(view, newProgress);
}
});
web_view.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void shouldOverrideUrlLoading(Webview view, String url){
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
return super.shouldOverrideUrlLoading(view,url);
}
})
webSettings.setJavaScriptEnabled(true);
web_view.loadUrl("http://someurl.com");
I have also done this:
#Override
public void shouldOverrideUrlLoading(Webview view, String url){
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
but both of them result in '404 not found'.
Any help please..
Prepare the layout to show when an error occurred instead of Web Page (a dirty 'page not found message') The layout has one button, "RELOAD" with some guide messages.
If an error occurred, Remember using boolean and show the layout we prepare.
If user click "RELOAD" button, set mbErrorOccured to false. And Set mbReloadPressed to true.
if mbErrorOccured is false and mbReloadPressed is true, it means webview loaded page successfully. 'Cause if error occurred again, mbErrorOccured will be set true on onReceivedError(...)
Here is my full source. Check this out.
public class MyWebViewActivity extends ActionBarActivity implements OnClickListener {
private final String TAG = MyWebViewActivity.class.getSimpleName();
private WebView mWebView = null;
private final String URL = "http://www.google.com";
private LinearLayout mlLayoutRequestError = null;
private Handler mhErrorLayoutHide = null;
private boolean mbErrorOccured = false;
private boolean mbReloadPressed = false;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
((Button) findViewById(R.id.btnRetry)).setOnClickListener(this);
mlLayoutRequestError = (LinearLayout) findViewById(R.id.lLayoutRequestError);
mhErrorLayoutHide = getErrorLayoutHideHandler();
mWebView = (WebView) findViewById(R.id.webviewMain);
mWebView.setWebViewClient(new MyWebViewClient());
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
mWebView.setWebChromeClient(getChromeClient());
mWebView.loadUrl(URL);
}
#Override
public boolean onSupportNavigateUp() {
return super.onSupportNavigateUp();
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btnRetry) {
if (!mbErrorOccured) {
return;
}
mbReloadPressed = true;
mWebView.reload();
mbErrorOccured = false;
}
}
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}
else {
finish();
}
super.onBackPressed();
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
if (mbErrorOccured == false && mbReloadPressed) {
hideErrorLayout();
mbReloadPressed = false;
}
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mbErrorOccured = true;
showErrorLayout();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
private WebChromeClient getChromeClient() {
final ProgressDialog progressDialog = new ProgressDialog(MyWebViewActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
return new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
};
}
private void showErrorLayout() {
mlLayoutRequestError.setVisibility(View.VISIBLE);
}
private void hideErrorLayout() {
mhErrorLayoutHide.sendEmptyMessageDelayed(10000, 200);
}
private Handler getErrorLayoutHideHandler() {
return new Handler() {
#Override
public void handleMessage(Message msg) {
mlLayoutRequestError.setVisibility(View.GONE);
super.handleMessage(msg);
}
};
}
}
try this... this is the code from AS default webview activity, generated by AS.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.google);
initInstances();
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
}
sidenote :-->> iinitInstances() is referring to nagivation drawer on the left, could be ignored.
I'm loading data in a webview using loadDataWithBaseURL from a string. I want to implement a listener which tells me when the data is loaded completely so that i may proceed with my code.
I've searched a lot but could not find any relevant topic
if (new String(rowData.getResource().getData()) != null) webView.loadDataWithBaseURL("", new String(rowData.getResource().getData()), "text/html", "UTF-8", "about:blank");
Use WebViewClient and implement onPageStarted,onPageFinished to know the status of the webview.
ProgressDialog progressDialog = new ProgressDialog.show(this);
progressDialog.setMessage("Loading WebPage...");
class CustomWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.dismiss();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}
webview.setWebViewClient(new CustomWebViewClient());
Try this Tested working demo.
public class URLActivity extends Activity {
private ProgressDialog progress;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
WebView webView = (WebView) findViewById(R.id.webView);
webView.setInitialScale(0);
webView.setWebViewClient(new WebViewClient());
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
webView.loadDataWithBaseURL("", new String(rowData.getResource().getData()), "text/html", "UTF-8", "about:blank");
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress > 0) {
showProgressDialog("Please Wait");
}
if (newProgress >= 100) {
hideProgressDialog();
// data load successfully. you can go further.
}
}
});
}
public void showProgressDialog(final String msg) {
runOnUiThread(new Runnable() {
public void run() {
if (progress == null || !progress.isShowing()) {
progress = ProgressDialog.show(URLActivity.this, "", msg);
}
}
});
}
public void hideProgressDialog() {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
if (progress.isShowing())
progress.dismiss();
} catch (Throwable e) {
}
}
});
}
}