TableRow TableLayout table row add delete remove removeview addview Android example

TableRow TableLayout table row add delete remove removeview addview get table row index indexOfChild create table row dynamically TextView dynamically Android example
Main.java

TableLayout table = (TableLayout)findViewById(R.id.table);
TableRow row = (TableRow)findViewById(R.id.row);
 
// get table row index android.
int nIndex = table.indexOfChild(row);
table.removeView(row); // invisible and height == 0
// add row into same place 
table.addView(row, nIndex); // visible
 
// add row into certain position
table.addView(row, 3); // visible
 
// create new TableRow dynamically 
TableRow tr = new TableRow(this);
         tr.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
 
// create own function for append TableRow
   private void appendRow(TableLayout table) {
        TableRow row = new TableRow(this);
 
        TextView hLabel = new TextView(this);
        hLabel.setText("Some text");
        hLabel.setPadding(3, 3, 3, 3);
 
        TextView hNextLabel = new TextView(this);
        hNextLabel.setText("Next text");
        hNextLabel.setPadding(3, 3, 3, 3);
        hNextLabel.setGravity(Gravity.RIGHT | Gravity.TOP);
 
        row.addView(hLabel, new TableRow.LayoutParams(1));
        row.addView(hNextLabel, new TableRow.LayoutParams());
 
        table.addView(row, new TableLayout.LayoutParams());
    }

main.xml ScrollView, TableLayout, TableRow, TextView Android xml layout example

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    <TableLayout
                android:id="@+id/table"
                  android:layout_width="fill_parent"
                 android:layout_height="fill_parent">
        <TableRow>
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Row index 0"/>
        </TableRow>
        <TableRow android:id="@+id/row">
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Row index 1"/>
        </TableRow>
        <TableRow>
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Row index 2"/>
        </TableRow>
        <TableRow>
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Row index 3"/>
        </TableRow>
		</TableLayout>
</ScrollView>
This entry was posted in Android Examples Code and tagged , , , , , . Bookmark the permalink.


Comments are closed.



23,142 views