Sharkbird
Sharkbird

Reputation: 61

How can I set the visibility of a button from a custom surfaceview?

So basically I'm making a really simple game on android. Within my Activity I have a SurfaceView and some Buttons labelled play and restart. I set the visibility of the Buttons accordingly so that when the game is in the menu state it sets the play button to VISIBLE and in the game state it's set to GONE. The restart button is set to GONE throughout until the player loses the game. My problem is, when game is in it's lose state, how can the SurfaceView access the restart button from the main Activity to set it to VISIBLE? Is this how it's supposed to work, or is there another way? I'm kinda new to android, but I think this is pretty important so the user can really navigate through the app, especially in games. Maybe there's some beginner stuff I have to go back to?

Upvotes: 1

Views: 152

Answers (2)

Bondax
Bondax

Reputation: 3163

Depending on your class structure, set a member variable to hold the reference to your button, or, if you are holding a context or the main layout, use Activity.findViewById(ButtonID) or a parent views View.findViewByID(ButtonID). Or you can introduce a new e.g. OnGameState interface whose implementations get notified when the game state changes. One of those implementations would then set the button's state.

Upvotes: 1

Vinay W
Vinay W

Reputation: 10190

write a new restart button programmatically, add it to your "lost/game-over" activity's layout . Write the appropriate code for its onClick()(something like finish() the present activity and launch the "new-game" activity) .

Upvotes: 0

Related Questions