aspose file tools*
The moose likes Android and the fly likes Add Button to Bouncing Ball app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Add Button to Bouncing Ball app" Watch "Add Button to Bouncing Ball app" New topic
Author

Add Button to Bouncing Ball app

Jelle Wever
Greenhorn

Joined: Dec 30, 2012
Posts: 3
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?



Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
What does "that does not work" mean? What did you expect to happen, and what did happen instead?


Ping & DNS - my free Android networking tools app
Jelle Wever
Greenhorn

Joined: Dec 30, 2012
Posts: 3
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
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
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

Joined: Dec 30, 2012
Posts: 3
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add Button to Bouncing Ball app