Start animation on page loading, stop page loading

I have a MenuItem in my ActionBar , which is a reload icon. My Activity has a WebView , and I want the icon to start the animation when the WebView starts loading the web page and stops when it is finished. This includes clicking on links on a downloadable site. What I have so far works the first time I open a webpage, but if I leave Activity and load another webpage, the reload icon seems to double or I get a NullReference exception, T27>

enter image description here

Here is my code:

 public class Browser extends SherlockFragmentActivity { private MenuItem refreshItem; private WebView mWebView; @Override public void onCreate(final Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.browser); mWebView = (WebView)findViewById(R.id.webview); mWebView.getSettings().setSupportZoom(true); mWebView.getSettings().setBuiltInZoomControls(true); mWebView.loadUrl("http://www.google.com"); mWebView.setWebViewClient(new WebBrowserClient()); mWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { //if (!isFinishing() && progress == 100 && refreshItem != null && refreshItem.getActionView() != null) //{ //refreshItem.getActionView().clearAnimation(); //refreshItem.setActionView(null); //} } }); } private class WebBrowserClient extends WebViewClient { @Override public void onLoadResource(WebView view, String url) { //StartAnimation(); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { StartAnimation(); } @Override public void onPageFinished(WebView view, String url) { if (refreshItem != null && refreshItem.getActionView() != null) { refreshItem.getActionView().clearAnimation(); refreshItem.setActionView(null); } } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } private void StartAnimation() { final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null); final Animation rotation = AnimationUtils.loadAnimation(this, R.anim.refresh); ivRefresh.startAnimation(rotation); refreshItem.setActionView(ivRefresh); } @Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.menu, menu); refreshItem = menu.findItem(R.id.refresh); return super.onCreateOptionsMenu(menu); } } 

The commented code is different from the way I tried to get it to work.

UPDATE:

After debugging this more, I set a breakpoint in the StartAnimation function, and sometimes it reached 7 times in a row, and in other cases it didn't. That doesn't make sense, as it should work for me. Perplexity ...

SOLUTION (SORT OF):

Updating the StartAnimation () function for this seems to fix this problem, but seems to be more suited to solve the ribbon problem:

 private void StartAnimation() { if (refreshItem != null && refreshItem.getActionView() == null) { final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null); final Animation rotation = AnimationUtils.loadAnimation(this, R.anim.refresh); ivRefresh.startAnimation(rotation); refreshItem.setActionView(ivRefresh); } } 
+8
android android-webview menuitem android-animation
source share
3 answers

To create a smooth ui experience, the animation of the “loading wheel” should begin when the user clicks on the link, not on the loading of a new page, and ends when a new page is loaded. This makes the application more responsive to the user, and the behavior described in Dolphin Browser is shown as an example.

To implement this, you need to listen for clicks on user links in WebView. How to do this, this answer already says here: SO:

Detect click html button through javascript in android webview

You must confirm that the click is essentially linked to a link to a new page. The clicked HTML element can be found using the WebView class HitTestResult . This answer to SO gives you detailed information on how to check the value of HitTestResult:

Get click event from webpage in my android app.

You start your animation from your onClick () method and end it using the WebViewClient onPageFinished () method, and if the new page does not load, also in onReceivedError ().

+2
source share

Make the Animation variable a member of your Browser class and make sure you stop the animation before exiting.

0
source share

You can use the concept of threads.

Before loading the URL into the Web View, you show the loading animation and when the URL is loaded, stop the animation.

You need to use two streams: one for the progress bar (e.g. URL loading) and the other UI thread for your animation.

Find him!

0
source share

All Articles