Chủ Nhật, 9 tháng 7, 2017

Add TableRow to TableLayout programmatically

***Main layout:


<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <
TableLayout
       
android:id="@+id/hungtvTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <
TableRow
           
android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background_menu_hover"
            android:focusable="true" >
            <
TextView
               
android:id="@+id/tableCell1"
                android:layout_width="wrap_content"
                android:text="xxx"
                android:layout_column="0"
                android:layout_weight="1"
                android:textColor="@color/black"  />

            <
TextView
               
android:id="@+id/tableCell2"
                android:layout_width="wrap_content"
                android:text="xxx"
                android:layout_column="1"
                android:layout_weight="1"
                android:textColor="@color/black"  />

            <
TextView
               
android:id="@+id/tableCell3"
                android:layout_width="wrap_content"
                android:text="xxx"
                android:layout_column="2"
                android:layout_weight="1"
                android:textColor="@color/black"  />

            <
TextView
               
android:id="@+id/tableCell4"
                android:layout_width="wrap_content"
                android:text="xxx"
                android:layout_column="3"
                android:layout_weight="1"
                android:textColor="@color/black"  />
        </
TableRow>
    </
TableLayout>
</
ScrollView>


***Row layout:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true" >
    <
TextView
       
android:id="@+id/tableCell1"
        android:layout_width="wrap_content"
        android:text="xxx"
        android:layout_column="0"
        android:layout_weight="1"
        android:textColor="@color/black"  />

    <
TextView
       
android:id="@+id/tableCell2"
        android:layout_width="wrap_content"
        android:text="xxx"
        android:layout_column="1"
        android:layout_weight="1"
        android:textColor="@color/black"  />

    <
TextView
       
android:id="@+id/tableCell3"
        android:layout_width="wrap_content"
        android:text="xxx"
        android:layout_column="2"
        android:layout_weight="1"
        android:textColor="@color/black"  />

    <
TextView
       
android:id="@+id/tableCell4"
        android:layout_width="wrap_content"
        android:text="xxx"
        android:layout_column="3"
        android:layout_weight="1"
        android:textColor="@color/black"  />
</
TableRow>


***Code:

strings = new ArrayList<>();
for (int index = 0; index < 80; index++) {
    strings.add("hungtv " + index);
}

for (String stringValue : strings) {
    final TableLayout detailsTable = (TableLayout) view.findViewById(R.id.hungtvTable);
    final TableRow tableRow = (TableRow) getActivity()
            .getLayoutInflater().inflate(R.layout.tablerow_layout, null);
    TextView tv;

    tv = (TextView) tableRow.findViewById(R.id.tableCell1);
    tv.setText(stringValue);
   
    tv = (TextView) tableRow.findViewById(R.id.tableCell2);
    tv.setText(stringValue);
   
    tv = (TextView) tableRow.findViewById(R.id.tableCell3);
    tv.setText(stringValue);
   
    tv = (TextView) tableRow.findViewById(R.id.tableCell4);
    tv.setText(stringValue);

    //Add row to the table
   
detailsTable.addView(tableRow);
}


***Result: