Code example for TableLayout

Methods: setOrientationaddViewsetShrinkAllColumnssetStretchAllColumns, setLayoutParams

0
		super.onCreate(savedInstanceState);
		bv = new BackgroundView(this);
		 
		duel = new Duel("P1", "P2");
		 
		layout = new TableLayout(this);
		frame = new FrameLayout(this);
				 
		layout.setOrientation(TableLayout.VERTICAL);
		layout.setShrinkAllColumns(false);
		layout.setStretchAllColumns(true);
		//frame.setLayoutParams(new FrameLayout.LayoutParams(GameRules.getScreenWidth(), FrameLayout.LayoutParams.FILL_PARENT)); 
		layout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
 
		 
		TableRow round = new TableRow(this);
		TextView roundLbl = new TextView(this);
		roundLbl.setPadding(10, 5, 0, 20);
		roundLbl.setText("Round " + duel.getRound());
		round.addView(roundLbl);
		 
		TableRow usernames = new TableRow(this);
		TextView p1username = new TextView(this);
		TextView p2username = new TextView(this);
		p1username.setText(duel.getPlayer1().getUsername());
		p2username.setText(duel.getPlayer2().getUsername());
		p1username.setGravity(Gravity.RIGHT);
		p2username.setGravity(Gravity.RIGHT);
		usernames.addView(p1username);
		usernames.addView(p2username);
		 
		TableRow rd1row = new TableRow(this);
		TextView rd1Lbl = new TextView(this);
		TextView p1r1 = new TextView(this);
		TextView p2r1 = new TextView(this);
		rd1Lbl.setText("Round 1");
		rd1Lbl.setPadding(10, 0, 0, 0);
		p2r1.setPadding(0, 0, 10, 0);
		p1r1.setText(duel.getPlayer1().getRd1score() +"");
		p2r1.setText(duel.getPlayer2().getRd1score() +"");
		p1r1.setGravity(Gravity.CENTER);
		p2r1.setGravity(Gravity.RIGHT);
		rd1row.addView(rd1Lbl);
		rd1row.addView(p1r1);
		rd1row.addView(p2r1);
 
		TableRow rd2row = new TableRow(this);
		TextView rd2Lbl = new TextView(this);
		TextView p1r2 = new TextView(this);
		TextView p2r2 = new TextView(this);
		rd2Lbl.setText("Round 2");
		rd2Lbl.setPadding(10, 0, 0, 0);
		p2r2.setPadding(0, 0, 10, 0);
		p1r2.setText(duel.getPlayer1().getRd2score() +"");
		p2r2.setText(duel.getPlayer2().getRd2score() +"");
		p1r2.setGravity(Gravity.CENTER);
		p2r2.setGravity(Gravity.RIGHT);
		rd2row.addView(rd2Lbl);
		rd2row.addView(p1r2);
		rd2row.addView(p2r2);
		 
		TableRow rd3row = new TableRow(this);
		TextView rd3Lbl = new TextView(this);
		TextView p1r3 = new TextView(this);
		TextView p2r3 = new TextView(this);
		rd3Lbl.setText("Round 3");
		rd3Lbl.setPadding(10, 0, 0, 0);
		p2r3.setPadding(0, 0, 10, 0);
		p1r3.setText(duel.getPlayer1().getRd3score() +"");
		p2r3.setText(duel.getPlayer2().getRd3score() +"");
		p1r3.setGravity(Gravity.CENTER);
		p2r3.setGravity(Gravity.RIGHT);
		rd3row.addView(rd3Lbl);
		rd3row.addView(p1r3);
		rd3row.addView(p2r3);
		 
		TableRow brrow = new TableRow(this);
		TextView rdbLbl = new TextView(this);
		TextView p1rb = new TextView(this);
		TextView p2rb = new TextView(this);
		rdbLbl.setText("Bonus Round");
		rdbLbl.setPadding(10, 0, 0, 0);
		p2rb.setPadding(0, 0, 10, 0);
		p1rb.setText(duel.getPlayer1().getBonusscore() +"");
		p2rb.setText(duel.getPlayer2().getBonusscore() +"");
		p1rb.setGravity(Gravity.CENTER);
		p2rb.setGravity(Gravity.RIGHT);
		brrow.addView(rdbLbl);
		brrow.addView(p1rb);
		brrow.addView(p2rb);
		 
		TableRow totalrow = new TableRow(this);
		TextView totalLbl = new TextView(this);
		TextView p1t = new TextView(this);
		TextView p2t = new TextView(this);
		totalLbl.setText("Total Score");
		totalLbl.setPadding(10, 0, 0, 0);
		p2t.setPadding(0, 0, 10, 0);
		p1t.setText(duel.getPlayer1().totalScore() +"");
		p2t.setText(duel.getPlayer2().totalScore() +"");
		p1t.setGravity(Gravity.CENTER);
		p2t.setGravity(Gravity.RIGHT);
		totalrow.addView(totalLbl);
		totalrow.addView(p1t);
		totalrow.addView(p2t);
		 
		TableRow vrow = new TableRow(this);
		View hline = new View(this);
		hline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
		hline.setBackgroundColor(Color.rgb(51, 51, 51));
		hline.setPadding(10, 0, 10, 0);
		vrow.addView(hline);
		 
		round.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
		usernames.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
		rd1row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
		rd2row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
		rd3row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
		 
		Button start = new Button(this);
		start.setText("Next");
		TableRow buttonrow = new TableRow(this);
		 
		 
		TableRow.LayoutParams buttonrowp = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT);
		buttonrowp.span = 3;
		start.setLayoutParams(buttonrowp);
		buttonrow.addView(start);
 
		layout.addView(round);
		layout.addView(usernames);
		layout.addView(rd1row);
		layout.addView(rd2row);
		layout.addView(rd3row);
		layout.addView(brrow);
		layout.addView(totalrow);
		//layout.addView(vrow); 
		layout.addView(buttonrow);
	    frame.addView(bv);
		frame.addView(layout);
		setContentView(frame);