-1

I am creating TableLayout,

xml code of TableLayout:

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

I want to add n no of rows dynamically but I want only 2 columns in each row like this image

TableView with 2 columns dynamically

I have used android:strechColumns in layout file but not succeed.

how can I solve this problem dynamically.?

Please help...

Thanks in advance.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437

2 Answers2

4

Here is activity_table_layout_ex xml:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TableLayout
                    android:id="@+id/tableLayoutProduct"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:background="@android:color/white"
                    android:stretchColumns="*" >
                </TableLayout>
            </LinearLayout>
        </HorizontalScrollView>
    </ScrollView>

</LinearLayout>

And here is your activity code:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TableLayoutEx extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table_layout_ex);

         TableLayout tableLayoutProduct = (TableLayout) findViewById(R.id.tableLayoutProduct);

            String[] headingTitle = new String[] { "Col1", "Col2", "Col3", "Col4" };

            TableRow tableRow = new TableRow(this);
            tableRow.setBackgroundColor(getResources().getColor(android.R.color.black));

            for (int i = 0; i < headingTitle.length; i++) {
                TextView textView = new TextView(this);
                textView.setText(headingTitle[i]);
                textView.setTextColor(getResources().getColor(android.R.color.white));
                textView.setPadding(5, 10, 5, 10);
                tableRow.addView(textView);
            }
            tableLayoutProduct.addView(tableRow);
    }

}

Try this working code. You can achieve that by following this example.

Homam
  • 5,018
  • 4
  • 36
  • 39
  • @PratikButani I have tested this code on sample project. It is running perfectly. What is the problem that you are facing? – Homam Oct 04 '13 at 08:47
  • Trying creating new project. If you created a project with MainActivity, copy the above xml to your activity_main.xml. And in your MainActivity copy the above code from TableLayout till the end. Import necessary packages using ctrl+shift+o and then run the project. – Homam Oct 04 '13 at 08:52
1
 <TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="3"
    android:id="@+id/tableLayout"
     >

    <TableRow>
        <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:weightSum="3">

            <View android:layout_width="0dp" 
                android:layout_height="wrap_content" 
                android:layout_weight="1"
            </View>
            <View android:layout_width="0dp" 
                android:layout_height="wrap_content" 
                android:layout_weight="1"
            </View>
            <View android:layout_width="0dp" 
                android:layout_height="wrap_content" 
                android:layout_weight="1"
            </View>
        </LinearLayout>
     </TableLayout>

 TableLayout tl=(TableLayout)findViewById(R.id.tableLayout);    
 TableRow tr1 = new TableRow(this);
 tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
 t1.addView(tr1);
 LinearLayout ll = new LinearLayout(this);
 ll.setOrientation(LinearLayout.HORIZONTAL);
//LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0dp,LayoutParams.WRAP_CONTENT, 3);
  tr1.addView(ll, params);
  View v1= new View(this);
  View v2= new View(this);
  View v3= new View(this);
  ll.addView(v1, new LayoutParams(0dp, LayoutParams.WRAP_CONTENT,1));
  ll.addView(v2, new LayoutParams(0dp, LayoutParams.WRAP_CONTENT,1));
  ll.addView(v3, new LayoutParams(0dp, LayoutParams.WRAP_CONTENT,1));
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96