Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Animation doesn't work on a button click in Android app

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am new to Android and trying to improve my learning. I am programming an Android app that shows animation of the images stored in image array list. The animation works fine when the activity is fired (on Create). In my app, there is a button with on click listener. I also want to run the animation when this button is clicked. However, when I click the button, the animation disappears. I am unable to understand why and logcat doesn't give any useful hints for me to try to debug.

Is it possible to run the same animation on button click? If yes, is there someone who could help me to fix this issue?

Here is my app code.

Main Activity.java



Activity_main.XML



MyScroller.java class



MyPageAdapter.java class:



and My_page_item.XML



and here is some log via "logcat" when I click the button and the animation disappears.

 
Master Rancher
Posts: 4591
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some debugging ideas:
Comment out the call to animateImages in the onCreate and only call it in the listener.  To test if there is some state that only allows the animation to run once.
Add some print statements to show where the code is executing and what the values of variables are as they change and are used.
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestion!

I did as you suggested. That is, commented out "animate images" method in OnCreate. The animation now runs on button click but only once, meaning when I click on the button again (and again...), the animation disappears. Thus, you were right! Indeed, there maybe some state that allows animation to run only once. I failed to understand why this is happening?

This is what happened in the logcat when I click on the button the first time (animation runs in this case):



and when I click again on the button, the animation disappears, these lines appear:

 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One field that keeps its value is viewPager on line 42.  Try removing the test for null so it always gets a new instance.
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried. I changed



to



Unfortunately, it didn't help and I got the same behavior that, the animation runs only once, either in OnCreate or via ButtonClick.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the ViewPager class have any memory of what has been viewed?

Sorry, I misread line 42.  It was only a safety test.  line 11 is where viewPager gets its value.  What about resetting the currentItem to 0?
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No worries

Sorry, but I don't understand which line 11 you meant? In MainActivity.java, line 11, there is no currentItem Perhaps, you meant line 51? If I reset currentPage = 0 here, the animation freezes on the first image and doesn't move forward.

Apologies if I misunderstood you.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 11 is where viewPager is assigned a value:

What about setting current Item to 0?  Something like this:


If I reset currentPage = 0 here, the animation freezes on the first image


That sounds like progress.  Before  the animation disappeared
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification. I tried:



The animation that was running in onCreate is now stopped/freezed at image 1. Additionally, the animation still disappears on button click so no progress here.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you setting the value to 0?  If it gets reset for each image, only the first image will ever be shown.

Where is currentPage declared?
Lines 49-50 wrap its value around to 0 when it gets to the end.    Does that work?  Does the animation wrap around at the end?  Ie does it go forever?
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am changing these lines of code from



to



Lines 48 - 53 in MainActivity.java. Am I doing something wrong?

 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Am I doing something wrong?


Yes, the code is always only showing the first image.  

Did the original code run continuously, wrapping around at the end of the list of images back to the first image?
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the original code runs the animation continuously. It starts from the first image, goes to the last, and starts from the first image again. Thus, it animates in a continuous loop.

The problem, as we discovered is, it runs only once. If I call the method twice, only the first method call would run the continuous animation, let it be in OnCreate or via button click.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add print statements to all the methods that are called so you can see that they are all called as expected.  
Compare what is printed for when it works and what is printed when it fails.

The posted print outs show that destroyItem is called at end of good test and then is called continually in failed test.
When and why is destroyItem called?
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Compare what is printed for when it works and what is printed when it fails.


The funny (and annoying) thing is I only see, , and in the stack trace log when animation works and only destroyItem() when things don't work. I already put this in the thread as one of your answers.

When and why is destroyItem called?


Please look at MyScroller.java and MyPageAdapter.java. Especially, in MyPageAdapter.java, there are , ("load in gallery" is just a log message here) and are the main components running the animation. I tried commenting destroyItem(), the app crashes and won't start.

Scroller and PageAdapter set up the animation and above three components run it.

Hope it makes sense?
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is destroyItem called?  Can you control when it is called?  Maybe if it is not called it will work differently.
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
destroyItem() is called to remove/unload the object (here, the image object). It's like loading and unloading to the next scroll until the animation reaches to the last image.

If look at the method:



It removes the object from the LinearLayout (container) at a certain position (image 1, 2, 3 and so on..).

Can you control when it is called?


How do I do that?

As I said, I have tried commenting destroyItem() method in MyPageAdapter.java class, and ran the app again, the app crashes and won't start.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Feed this to Google:  destroyitem viewpager android
and read through all the discussions for some ideas.


Was the first call to destroyItem in response to a user's action after the animation had run for a while?
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Was the first call to destroyItem in response to a user's action after the animation had run for a while?


No. It is mainly to unload image so that the next image can be loaded and ViewPager scrolls to the next page (image).

Please consider looking at MyScroller and PagerAdapter classes.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of your posts of the logcat shows several calls to instantiateItem() followed by one call to destroyItem().
Then the next posting of the logcat shows only calls to destroyItem()

It is hard to guess what was happening on the device with only those small sections of the logcat posted here

Can you run the code again and allow the animation to wrap around at least once and then somehow show a  call  to destroyItem().
Copy that contents of the logcat and paste it here.  


What causes destroyItem() to be called?
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is hard to guess what was happening on the device with only those small sections of the logcat posted here


The code loads and unloads images, during this, instantiateItem() and destroyItem() are called.

What causes destroyItem() to be called?


The button click. When I put the animation method under onClick Listener then only destroyItem() is called and showed n logcat.

Running the code again would show the same behaviour as explained in the above.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am unable to do any testing to see what happens.  I am relying on you to do all the testing.  Just talking about the problem is no help.


Can you run the code again and allow the animation to wrap around at least once and then somehow show a  call  to destroyItem().
Copy that contents of the logcat and paste it here.  

 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, just talking will not solve the problem. Here is the log (full iteration) when I load first five images.


com.example.animationtest D/ImageViewPage: load in gallery:img_5.png#end

I noticed that, when the animation starts, images 2 and 3 are skipped and not shown at all!! When I animated for 15 images, the animation in the first loop, starts from image 4, and when it restarts again (full loop), then it starts from image 1.

I am unable to understand why ?
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am unable to understand why ?


Have you added print statements to the code so you can see when the various methods are called and what the values of the variables are?
For example add this in the run method:

The output will be shown in the logcat.

Why doesn't the contents of the logcat have the date/time.  Here is a copy of one of my logcat dumps:


09-20 19:53:05.083: I/System.out(8046): >>>>>>>>>> Started at Fri Sep 20 19:53:05 GMT 2013
09-20 19:53:05.147: I/System.out(8046): screen size=Point(1024, 552), inches=7.281629740932256
09-20 19:53:05.155: I/System.out(8046):  xdpi=159.5681, ydpi=160.42105  wPix=1024, hPix=552,  test48dp=48, dp=48, density=1.0
09-20 19:53:05.219: I/System.out(8046): cf to sk dist=20.003796, bearing=-152.31781, miles2=6466.4526  [1.0406721E7, 37.526474, 142.32643]
09-20 19:53:06.959: I/System.out(8046): onCreateOptionsMenu
09-20 19:53:06.975: I/System.out(8046): onPrepareOptionsMenu
09-20 19:53:07.135: D/gralloc_vbox86(8046): Emulator without GPU emulation detected.
09-20 19:53:07.519: I/Choreographer(8046): Skipped 45 frames!  The application may be doing too much work on its main thread.
09-20 19:53:17.851: I/System.out(8046): onPrepareOptionsMenu
09-20 19:53:22.463: I/System.out(8046): onOptionsItemSelected item=Show Fragment menu
09-20 19:53:22.723: I/System.out(8046): MDF onCreate() passed=passed string
09-20 19:53:24.347: I/System.out(8046): onItemSelected arg1=android.widget.TextView{a6645b10 V.ED.... ......ID 4,10-60,32 #1020014 android:id/text1}, pos=0, is=0
09-20 19:53:35.575: I/System.out(8046): onItemSelected arg1=android.widget.TextView{a6652f18 V.ED.... ......I. 4,10-60,32 #1020014 android:id/text1}, pos=4, is=4
09-20 19:53:40.875: I/System.out(8046): setChoice s=item4
09-20 19:53:50.163: I/Choreographer(8046): Skipped 48 frames!  The application may be doing too much work on its main thread.
09-20 19:53:53.819: I/System.out(8046): <<<<<<<<<<<<< Stopped at Fri Sep 20 19:53:53 GMT 2013

 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the logcat output after putting print statement in the run() method.



I remove the datatime to make log more readable (no long lines), I guess it's not that relevant for the code debugging. Is it?
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting that the printed cp=  value does not correlate with the position = [0] value???
Also the cP= value is never 0?

Where in that test run was the button pressed?  I do not see any print outs showing when the onClick method was called?
Where is the solid list of calls to destroyItem that was shown in an earlier post?
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made some progress!

In the beginning of this method:



I load the images like this:



When this method is called in OnCreate(), all seems to be working ok. But, when called, inside button click listener. Then this happens:



It means, "myImages size" becomes zero and an infinite while loop is initiated because of this:



I moved URL loading in OnCreate() and now animation works also on Button click !! (Yuhu !) But there is a bump in animation, which means, image 1 and 2 move too quickly and then bump, and then 3,4 and so on...

I can't figure out why this is happening!
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are the print statements that print out to the logcat.  I do not see any in the code.
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you the bottom of the logcat where myImages size and page number is mentioned? That is, print statement.

Do you some suggestions regarding code tweaking apart from questions?
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I can not understand what the code is doing when looking at the logcat output, since the output is not from the posted code.

Where is the code that adds anything to myImages?  I do not see any add statements for myImages.
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Where is the code that adds anything to myImages?  I do not see any add statements for myImages



It's  here.



The reason behind posting the code was, that folks can also reproduce the issue at their side. Did you try?
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Did you try?


No, it is a big job to set up Android testing.  Often OPs don't post all the code and a lot of time is wasted getting the code in synch.

For example the posted code does not have the snippet of code that you just posted that shows the call to the add method.

And there isn't any code posted with the print statements that you have shown the output from in the logcat.
 
Pete Alas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like we are heading towards the dead-end Questioning and answering certainly doesn't solve the problem.

I did as you suggested, have been answering your questions. But, if things are still unclear then my bad.
 
Norm Radder
Master Rancher
Posts: 4591
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I did as you suggested


There is some misunderstanding about what I suggested.  For example I suggested that you add print statements to all the methods to show where the code executed and what the contents of the variable were as the code executed.  Look at the logcat extract I posted.  There are lots of print statements that show what methods were being executed and what was in some of the variables.  
Your logcats don't show as much.
Then there was a mismatch between the logcat contents that were posted and the code that was supposed to go with that logcat did not have any print statements.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic