What you learn: You will learn how to add Rows/Lines to a TableLayout during Runtime of your application.
Difficulty: 1.5 of 5
What it will look like:
[align=center]
[/align]
Description: The following code-snippet enables you to dynamically add rows to a TableLayout:
Using java Syntax Highlighting
- this.setContentView(R.layout.main);
- /* Find Tablelayout defined in main.xml */
- TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
- /* Create a new row to be added. */
- TableRow tr = new TableRow(this);
- tr.setLayoutParams(new LayoutParams(
- LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT));
- /* Create a Button to be the row-content. */
- Button b = new Button(this);
- b.setText("Dynamic Button");
- b.setLayoutParams(new LayoutParams(
- LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT));
- /* Add Button to row. */
- tr.addView(b);
- /* Add row to TableLayout. */
- tl.addView(tr,new TableLayout.LayoutParams(
- LayoutParams.FILL_PARENT,
- LayoutParams.WRAP_CONTENT));
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Where my main.xml was:
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- id="@+id/myTableLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TableRow
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button android:text="Static Button"/>
- </TableRow>
- </TableLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
[align=center]Thats it
[/align]
Regards,
plusminus







