• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Add Button to Bouncing Ball app

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to add a button to a bouncing ball program. The button shows up in the layout, thats all fine.
Now i want to manipulate some of the variables in the AnimatedView class, but that does not work.
I believe that is because it is running in a seperate thread. Anyone can help here?



 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does "that does not work" mean? What did you expect to happen, and what did happen instead?
 
Jelle Wever
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:What does "that does not work" mean? What did you expect to happen, and what did happen instead?



Right, to be more clear: i am trying to achieve that by clicking the button i can manipulate the xVelocity and yVelocity variables (thus making the speed by which the ball moves faster) - i do that now by calling speedUpBall() method after creating a instance of the AnimatedView class.

I know this is wrong, because likely there is allready an instance of that class running after the xml was inflated with setContentView..
So i need a way to get to that instance (which is also running in a seperate thread of execution).

But how to do that i do not know. Hope this is clearer now
Right now clicking the button has no effect at all.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, you shouldn't create a new AnimatedView, you should retrieve the instance Android creates for you. You'd do that in the same way that you retrieve the instance of the button.

What you also need to do is to attach the OnClickListener to the button. As it is, the code will never be called, as it is not attached to any object. I prefer making the listener an anonymous inner class rather than having the Activity implement that interface, though.
 
Jelle Wever
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Right, you shouldn't create a new AnimatedView, you should retrieve the instance Android creates for you. You'd do that in the same way that you retrieve the instance of the button.

What you also need to do is to attach the OnClickListener to the button. As it is, the code will never be called, as it is not attached to any object. I prefer making the listener an anonymous inner class rather than having the Activity implement that interface, though.



This is great, thank you kindly Ulf!
Got it working like you said:
 
Your mother is a hamster and your father smells of tiny ads!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic