Add Button on custom View in Android

11,482

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 { //...
Share:
11,482
degude
Author by

degude

Updated on June 09, 2022

Comments

  • degude
    degude almost 2 years

    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?