Flynn
Flynn

Reputation: 6181

Android Button Reference?

So I'm having some issues with a method I'm going to be using to change a button's color based on a number received from the game engine (so if it returned 0 it would be red, 1 would change it to blue, 2 would change it to yellow) but I keep getting errors when trying to reference the button.

I'm referencing the buttons in this way:

Button x0y0 = (Button) findViewById(R.id.x0y0);

But I am getting an error, eclipse does not recognize

Any help on how I can get buttons into this program and have them change color based on what the engine returns to it?

Upvotes: 0

Views: 523

Answers (2)

Flo
Flo

Reputation: 27455

findViewById() is a public method of either the Activity or the View class. So you can only call it on/in those objects.

Upvotes: 0

Haphazard
Haphazard

Reputation: 10948

Your class ButtonColorUpdate will have to extend Activity to get access to findViewById(). It seems more likely, however, that you will want to pass the actual button to your ButtonColorUpdate class since it sounds like a helper class rather than a UI class.

You could also pass your activity to the ButtonColorUpdate class's constructor and then use that to get the ID of the button.

Upvotes: 4

Related Questions