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; }
android – How do I programmatically add buttons into layout one by one in several lines? – Stack Overflow
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.