Skip to content Skip to sidebar Skip to footer

Android Adding More Than One Textview To A Tablerow Programmatically

I have a LinearLayout inside a FrameLayout for tab purposes. And I'm trying to add TableRow's to the LinearLayout in the code. LinearLayout testLayout = (LinearLayout)findViewById(

Solution 1:

Have you imported this import android.widget.TableRow.LayoutParams

Below code works for me

TableLayouttl= (TableLayout) findViewById(R.id.spreadsheet);
  TableRowtr=newTableRow(this);
  LayoutParamslp=newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  tr.setLayoutParams(lp);

  TextViewtvLeft=newTextView(this);
  tvLeft.setLayoutParams(lp);
  tvLeft.setBackgroundColor(Color.WHITE);
  tvLeft.setText("OMG");
  TextViewtvCenter=newTextView(this);
  tvCenter.setLayoutParams(lp);
  tvCenter.setBackgroundColor(Color.WHITE);
  tvCenter.setText("It");
  TextViewtvRight=newTextView(this);
  tvRight.setLayoutParams(lp);
  tvRight.setBackgroundColor(Color.WHITE);
  tvRight.setText("WORKED!!!");

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

  tl.addView(tr, newTableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

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

Solution 2:

I think you want to create your TextView dynamically and should're getting error "removeView () parent." Here is a good solution for that:

TableViewtlSkills= (TableView) findViewById(R.id.myTableView);
if(listSkills.size() > 0) {
     TableRowtableRow=newTableRow(getContext());

     inti=0;
     for (Skills s : listSkills) {
     TextViewtextView=newTextView(getContext());
     textView.setText("" + s.getName());

     tableRow.addView(textView);

     if(i > 0) { 
          tlSkills.removeView(tableRow);//this is to avoid the error I mentioned above.
     }

     tlSkills.addView(tableRow);

     i++;
  }
}

Post a Comment for "Android Adding More Than One Textview To A Tablerow Programmatically"