Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add TableRow to TableLayout

Tags:

android

When a button is clicked, the following method is run:

public void createTableRow(View v) {
  TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
  TableRow tr = new TableRow(this);
  LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  tr.setLayoutParams(lp);

  TextView tvLeft = new TextView(this);
  tvLeft.setLayoutParams(lp);
  tvLeft.setBackgroundColor(Color.WHITE);
  tvLeft.setText("OMG");
  TextView tvCenter = new TextView(this);
  tvCenter.setLayoutParams(lp);
  tvCenter.setBackgroundColor(Color.WHITE);
  tvCenter.setText("It");
  TextView tvRight = new TextView(this);
  tvRight.setLayoutParams(lp);
  tvRight.setBackgroundColor(Color.WHITE);
  tvRight.setText("WORKED!!!");

  tr.addView(tvLeft);
  tr.addView(tvCenter);
  tr.addView(tvRight);

  tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}

R.id.spreadsheet is an xml TableLayout. I can see from debugging that the method is being accessed, but nothing is drawn to the screen. What gives? Do I need to reset the Content View somehow?

like image 398
dfetter88 Avatar asked Jul 08 '10 04:07

dfetter88


1 Answers

As it turns out, Eclipse is not always right. Ctrl+Shift+M made the wrong import. It had import android.view.ViewGroup.LayoutParams when it should have import android.widget.TableRow.LayoutParams

Everything works fine now.

like image 61
dfetter88 Avatar answered Nov 10 '22 17:11

dfetter88