Reputation: 2897
I want to divide space of TableRow in Android among the children equally. Here is the screen done by me so far.
I want the row of "text 1 text 1 text 1" to take equal amount of space. I want to do it from java code. My java code is as follows :
public class History extends Activity implements View.OnClickListener {
int counter=0;
TableLayout TL ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
Button b1 = (Button) findViewById(R.id.Button01);
b1.setOnClickListener(this);
}
public void onClick(View arg0) {
TableLayout TL = (TableLayout) findViewById(R.id.table_layout);
TableRow row = new TableRow(this);
counter++;
TextView t = new TextView(this);
t.setText("text " + counter);
TextView t1 = new TextView(this);
t1.setText("text " + counter);
TextView t2 = new TextView(this);
t2.setText("text " + counter);
// add the TextView and the CheckBox to the new TableRow
row.addView(t);
row.addView(t1);
row.addView(t2);
TL.addView(row,new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
I want to implement the following screen .
What can I do to achieve my goal?
Upvotes: 0
Views: 871
Reputation: 5266
You want to use weight
and weightSum
in the TableRow
and set our TextView
via TableLayout.LayoutParams
to do this. Here is some example code with relevant comments:
TableRow row = new TableRow(this);
row.setLayoutParams(
new TableLayout.LayoutParams((LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT))
// Set the weightSum of the row
row.setWeightSum(1f);
// Set the layout and weight for the columns. Note 0.5f gives us 0.5f+0.5f = 1f,
// the weightSum set above. (Using two rows as an example).
TableLayout.LayoutParams layoutParams =
new TableLayout.LayoutParams(0,
LayoutParams.LayoutParams.WRAP_CONTENT, 0.5f);
TextView t1 = new TextView(this);
t1.setText("text " + counter);
// Set our layoutParams
t1.setLayoutParams(layoutParams);
TextView t2 = new TextView(this);
t2.setText("text " + counter);
// Set our layoutParams
t2.setLayoutParams(layoutParams);
// Add views to row
row.addView(t1);
row.addView(t2);
// Add row to table
TL.addView(row);
Upvotes: 2