Add button programatically

With the help of stackoverflow I managed to add a button programatically. (Previously my code just created myTimerPanel and as that as the content view.

public class TimerActivity extends Activity implements OnClickListener {

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		FrameLayout game = new FrameLayout(this);
		TimerPanel myTimerPanel = new TimerPanel(this, timer1, timer2);
		LinearLayout gameWidgets = new LinearLayout (this);
		Button endGameButton = new Button(this);
		endGameButton.setWidth(300);
		endGameButton.setText("Stop");
		gameWidgets.addView(endGameButton); 
		game.addView(myTimerPanel);
		game.addView(gameWidgets);
		setContentView(game);
		endGameButton.setOnClickListener(this);
		gameOver = false;
	}

 

This entry was posted in Android. Bookmark the permalink.

2 Responses to Add button programatically

  1. android – How do I programmatically add buttons into layout one by one in several lines? – Stack Overflow

    • wpadmin says:

      It isn’t something I’ve tried but I think you would need to create programmatically a layout – probably a grid layout for your case – and within that create the buttons programmatically.

Leave a Reply

Your email address will not be published. Required fields are marked *