I have a little piece of code which adds a new row (4 columns) to an existing table when pressing the btnTest
-Button. Each field of this new row is filled with "0". However, the columns of the added rows are not evenly spread out (the columns are getting smaller towards the right side of the screen). I already created the first row of this table in the layout-file, and this first row is evenly spread out. Is there a way to space out the rows in the Java-code (like setLayoutWeight
or something)?
btnTest=(Button)findViewById(R.id.btnTest);
btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tr = new TableRow(ScoreScreen.this);
tvScore1 = new TextView(ScoreScreen.this);
tvScore2 = new TextView(ScoreScreen.this);
tvScore3 = new TextView(ScoreScreen.this);
tvScore4 = new TextView(ScoreScreen.this);
tvScore1.setText("0");
tvScore1.setTextSize(25);
tvScore1.setGravity(Gravity.CENTER);
tvScore1.setTextColor(Color.WHITE);
tvScore2.setText("0");
tvScore2.setTextSize(25);
tvScore2.setGravity(Gravity.CENTER);
tvScore2.setTextColor(Color.WHITE);
tvScore3.setText("0");
tvScore3.setTextSize(25);
tvScore3.setGravity(Gravity.CENTER);
tvScore3.setTextColor(Color.WHITE);
tvScore4.setText("0");
tvScore4.setTextSize(25);
tvScore4.setGravity(Gravity.CENTER);
tvScore4.setTextColor(Color.WHITE);
tr.addView(tvScore1);
tr.addView(tvScore2);
tr.addView(tvScore3);
tr.addView(tvScore4);
t2.addView(tr);
}
});
t2=(TableLayout)findViewById(R.id.t2);
t2.setColumnStretchable(0, true);
t2.setColumnStretchable(1, true);