Tvd
Tvd

Reputation: 4601

android Set Height of TableLayout

I have a TableLayout with just a single row (i.e. the Header Row) in XML. Other all rows I add dynamically. XML PART :

    <TableLayout android:id="@+id/list_table" 
    android:layout_width="match_parent" android:layout_height="wrap_content" 
    android:stretchColumns="1" android:background="#808000" **android:scrollbars="vertical">**
    <TableRow android:layout_margin="1dp" android:layout_height="wrap_content" 
    android:layout_width="wrap_content" android:id="@+id/list_head_Row">
        <TextView android:layout_width="wrap_content"  
        android:text="Col1" android:layout_height="wrap_content"></TextView>
        <TextView android:gravity="center" android:layout_width="wrap_content" android:text="Col2" android:layout_height="wrap_content"></TextView>
        <TextView android:layout_width="wrap_content" android:text="Col3"  android:layout_height="wrap_content"></TextView>
    </TableRow>
</TableLayout>

In Activity class, I add TableRows :

TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
// Add 3 TextViews
// Add Row to table
t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,             LayoutParams.WRAP_CONTENT));

With this much all rows gets added to the table and fills the screen. I want to set is, view only 4-5 rows and others can be scrolled.For that I added :

    int tableHgt = t1.getChildAt(0).getHeight() + (rowHgt *40);
    t1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, tableHgt));

With this I can't see any row at all except the header row added in xml. How can I make its height to see just 5 rows and othe all can be scrolled ? In TableLayout android:scrollbars="vertical" is also added.

Where am I going wrong ?

Any help is highly appreciated.

Thanks

Upvotes: 2

Views: 13463

Answers (1)

Selvin
Selvin

Reputation: 6797

why don't you use ListView ?

but ok ... back to your question put TableLayout in ScrollView and setup height in ScrollView

EDIT: code

activity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scrolltest);

    TableLayout t1 = (TableLayout) findViewById(R.id.list_table);

    for (int i = 0; i < 10; i++) {
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));           
        t1.addView(tr, new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));  
        TextView tv = new TextView(this);
        tr.addView(tv, new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tv.setText(Integer.toString(i));
        tv = new TextView(this);
        tr.addView(tv, new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tv.setText("Row number");
        tv = new TextView(this);
        tr.addView(tv, new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tv.setText(Integer.toString(i));        
    }
}

scrolltest.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="100dp"
    android:id="@+id/list_scroll">
    <TableLayout android:id="@+id/list_table"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:stretchColumns="1">
        <TableRow android:layout_margin="1dp" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:id="@+id/list_head_Row"
            android:background="#808000">
            <TextView android:layout_width="wrap_content" android:text="Col1"
                android:layout_height="wrap_content" />
            <TextView android:gravity="center" android:layout_width="wrap_content"
                android:text="Col2" android:layout_height="wrap_content" />
            <TextView android:layout_width="wrap_content" android:text="Col3"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>
</ScrollView>

Upvotes: 2

Related Questions