0

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 table layout (from xml).

I need to create the table layout as well as add data to it dynamically. Can anyone please provide some inputs?

For now, I have linear layout code in place which adds button from code behind one below the other, I need to place it under a tabular format now.

3 Answers 3

4

To add three buttons to TableRow use the code below

TableLayout tableLayout = new TableLayout(this);
    for (int i = 0; i < 10; i++)
    {
        TableRow tableRow = new TableRow(this);
        Button button = new Button(this);
        button.setText("1");
        tableRow.addView(button);

        button = new Button(this);
        button.setText("2");
        tableRow.addView(button);

        button = new Button(this);
        button.setText("3");
        tableRow.addView(button);

        tableLayout.addView(tableRow);
    }
    setContentView(tableLayout);
Sign up to request clarification or add additional context in comments.

2 Comments

You can add a for loop to add 3 buttons
I did some modifications as per my requirement and I was good to go... Thanks for helping me out. :)
1

Add the code below to your onCreate() method in you Activity class:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    TableLayout tableLayout = new TableLayout(this);
    for (int i = 0; i < 5; i++)
    {
        TableRow tableRow = new TableRow(this);

        for (int j = 0; j < 3; j++)
        {
            Button button = new Button(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.

5 Comments

Won't Layout parameters be used while coding GUI dynamically...?? Just asking out of curiosity... Also, what if I want to add buttons in such a way that I must have 3 columns in each row?
Yes you can use TableLayout.LayoutParams. and if you want a row ith 3 columns just add 3 views to the table row
Can you please be more specific..?? for adding 3 views to the row...? Because I would be adding each button in each iteration of the loop.. so couldn't add 3 views simultaneously. Could you please explain with an example?
Check the answe below
@HussainChachuliya copy the code and replace your Activities onCreate method with it
0

Add the following code below your init() method:

for (int i = 0; i < GetGlobal.totalrow; i++) {
            TableRow tbrow = new TableRow(this);
            // tbrow.setLayoutParams(tableRowParams);
            TextView t1v = new TextView(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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.