Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Button on custom View in Android

I have the following class

public class GameActivity extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      View gameView = new GameView(this);
      setContentView(gameView);
   }
}


and now I'd like to add a Button to my class GameView.

public GameView(Context context) {
        super(context);
// Code ...
}

I need this button during my game, so it should be alywas in front of all the other canvas' I'm drawing.
How can I do that?

like image 584
degude Avatar asked Apr 04 '12 15:04

degude


People also ask

How to add a button to action bar android?

Add Action Buttons To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


1 Answers

Do you want to create a new button?

Button b = new Button(context);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
                                   LayoutParams.WRAP_CONTENT)); 

gameView.addView(b);

Use ViewGroup as GameView parent insted of simple View

ViewGroup gameView = new GameView(this);


public class GameView extends ViewGroup { //...
like image 127
pleerock Avatar answered Sep 20 '22 11:09

pleerock