android Set Height of TableLayout

13,154

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>
Share:
13,154

Related videos on Youtube

Tvd
Author by

Tvd

Updated on June 04, 2022

Comments

  • Tvd
    Tvd almost 2 years

    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

  • Tvd
    Tvd about 13 years
    Selvin, after adding ScrollView, I get error in adding TableRows with the above source and the app shuts down. Why so ?
  • Tvd
    Tvd about 13 years
    Selvin, with this change, the error is gone, but can't see any rows also int tableHgt = t1.getChildAt(0).getHeight() + (rowHgt *40); scroller.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, tableHgt));
  • Selvin
    Selvin about 13 years
    @user_get_te_name still think that you should use ListView ... but See edit
  • Tvd
    Tvd about 13 years
    Thanks Selvin, the layout_height="100dp" did my work. Now my table contents are shown as expected, can also scroll using down& up arrow keys. Can't we have scrollbar like we can have in windows i.e. it remain visible, we can drag it on mouse scroll down/up. <ScrollView android:id="@+id/ScrollView01" android:layout_width="match_parent" android:layout_height="100dp" android:visibility="visible" android:scrollbars="vertical" android:scrollbarStyle="insideOverlay" android:focusable="true" android:focusableInTouchMode="true"> How to make it sta visible & scroll mouse /touch?
  • Selvin
    Selvin about 13 years
    @user_bla_bla esilo.pl/device1.png smth like this in red circle ? .... well it's android:fastScrollEnabled="true" in ListView
  • Tvd
    Tvd about 13 years
    Thanks. Its android:fadeScrollbars="false" in TableLayout that keeps scrollbar visible all time.