Creating Table Layout And Adding Rows From The Code Behind In Android
I need to create a table layout and add rows dynamically from Java code behind. I have already read questions here, but they are mentioning to add table rows in an already created
Solution 1:
To add three buttons to TableRow use the code below
TableLayouttableLayout=newTableLayout(this);
for (inti=0; i < 10; i++)
{
TableRowtableRow=newTableRow(this);
Buttonbutton=newButton(this);
button.setText("1");
tableRow.addView(button);
button = newButton(this);
button.setText("2");
tableRow.addView(button);
button = newButton(this);
button.setText("3");
tableRow.addView(button);
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
Solution 2:
Add the code below to your onCreate()
method in you Activity
class:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TableLayouttableLayout=newTableLayout(this);
for (inti=0; i < 5; i++)
{
TableRowtableRow=newTableRow(this);
for (intj=0; j < 3; j++)
{
Buttonbutton=newButton(this);
button.setText(""+j);
tableRow.addView(button);
}
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
}
The code will add five rows with three buttons with the text 1 to 3 to the table.
Solution 3:
Add the following code below your init()
method:
for (inti=0; i < GetGlobal.totalrow; i++) {
TableRowtbrow=newTableRow(this);
// tbrow.setLayoutParams(tableRowParams);TextViewt1v=newTextView(this);
t1v.setText(JSONParser.heading[i].replace('"', ' '));
t1v.setBackgroundResource(R.drawable.diamond_detail1);
t1v.setPadding(5, 3, 5, 3);
t1v.setMinHeight(50);
t1v.setTypeface(Typeface.SERIF);
t1v.setTextColor(Color.parseColor("#FFFFFF"));
t1v.setGravity(Gravity.FILL);
tbrow.addView(t1v);
Post a Comment for "Creating Table Layout And Adding Rows From The Code Behind In Android"