2

I'm trying to build a table in android dynamically. But when I attempt to inflate a view, for instance, table_cell.xml, and add it to my table row, nothing appears:

TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);

View tableCell = getActivity().getLayoutInflater()
                    .inflate(R.layout.table_cell, container, false);

TableRow tableRow = new TableRow(getActivity());

tableRow.addView(tableCell);
tableLayout.addView(tableRow);

On the other hand, if I just create a TextView manually, then add it to the row, it seems to work fine. Can anyone help me figure out why adding an inflated layout doesn't work?

jwBurnside
  • 849
  • 4
  • 31
  • 66
  • 1
    I think you should try with getActivity().getLayoutInflater() .inflate(R.layout.table_cell, tableRow, true); and you should not add the tableCell view on the tableRow manually in this case.. – Cata Jul 09 '14 at 19:03
  • @Cata - This worked, if you would like to write out an answer I will give you credit. – jwBurnside Jul 09 '14 at 19:16
  • Done, added below with a small extra explanation. Thanks! – Cata Jul 09 '14 at 19:23

2 Answers2

3

You should try with getActivity().getLayoutInflater() .inflate(R.layout.table_cell, tableRow, true);

And you should not add the tableCell view on the tableRow manually in this case as the inflator method takes care of that since we pass true ..

Cata
  • 11,133
  • 11
  • 65
  • 86
  • This works, thanks! In my case, I had to re-use an inflated layout multiple times. So what I did was set the last param to `false` .. and then was able to add the view on my own after giving it a unique id .. like so `inflatedLayout.setId(i); tableRow.addView(inflatedLayout);` .. thanks to http://stackoverflow.com/a/26619620/2162226 – Gene Bo Jul 13 '16 at 21:51
0

Try This You have to provide id for positioning.

TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);

View tableCell = getActivity().getLayoutInflater()
                .inflate(R.layout.table_cell, container, false);

TableRow tableRow = new TableRow(getActivity());

tableRow.addView(tableCell);
tableLayout.addView(tableRow,id);

or

TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);

View tableCell = getActivity().getLayoutInflater()
                .inflate(R.layout.table_cell, container, false);

TableRow tableRow = new TableRow(getActivity());

TableRow.LayoutParams LP = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);

tableRow.setLayoutParams(LP);
tablerow.setId(ID);


tableRow.addView(tableCell);
tableLayout.addView(tableRow);

or

TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);

View tableCell = getActivity().getLayoutInflater()
                .inflate(R.layout.table_cell, container, false);

TableRow tableRow = new TableRow(getActivity());


tableRow.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT)); 
tablerow.setId(ID);


tableRow.addView(tableCell);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
Punit
  • 667
  • 1
  • 7
  • 17