Search...

Thursday, December 1, 2011

How to insert row dynamically in TableLayout in android


(1)Create Layout:

my_layout.xml

<LinearLayout android:id="@+id/linearLayout"
android:layout_width="match_parent" android:layout_height="wrap_content"
>
<TableLayout android:id="@+id/Table"android:layout_height="fill_parent" android:layout_width="wrap_content"
android:stretchColumns="0,1"></TableLayout>
</LinearLayout>

(2)

public void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.my_layout.xml);

TableLayout testTable=(TableLayout) getView().findViewById(
R.id.Table);

TextView text=new TextView(this);
text.setText("Row1");
TableRow tr = null;

tr = new TableRow(getActivity());


tr.setPadding(0, 0, 0, 0);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));

tr.addView(text);
testTable.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

}

No comments:

Post a Comment