Daniel Jonsson
Daniel Jonsson

Reputation: 3893

How to create a table dynamically?

I know that it's possible to have an XML file with a TableLayout, and another XML file with a row. And then it's somehow possible to add rows, from the second file, with custom content in the java code to the table in the first file. But I can't find an example that shows how this is done. So, does anyone here know where I can find an example that shows this? I know that I have seen it before.

Upvotes: 1

Views: 1959

Answers (2)

Matthew
Matthew

Reputation: 44919

Once you have your TableLayout, you can inflate and add rows using:

TableRow row = (TableRow) LayoutInflater.from(this).inflate(
    R.layout.table_row, tableLayout, false);
tableLayout.addView(row);

Upvotes: 0

Cristian
Cristian

Reputation: 200090

First results using Google. Whatever... the idea is really simple:

  1. Get a reference of the TableLayout (either one declared in a XML file, or created by hand using new TableLayou(context))
  2. For each row you want to add, create a new TableRow object. Again, you can do so by using an already defined TableRow in an XML and inflate it; or you can just created by using the new operator.
  3. Add the items you want to put inside each row by using the addView method. Sometimes you will want to specify some TableRow.LayoutParams.
  4. Add the TableRow to the TableLayout.

Upvotes: 2

Related Questions