Question

So I have a TableLayout that I want to put inside a ScrollView. But I have a problem:

The TableLayout is initially empty and I programmatically add TableRows to it by tapping a button. This works fine if I put the TableLayout into the ScrollView normally.

But I want to put the TableLayout at the BOTTOM of the ScrollView. So every time I add a TableRow, the last cell will always be aligned to the bottom of the parent ScrollView. (kind of like how chat apps work - when you press send, your message is added at the bottom while all the other messages are pushed up).

What happens is that if I use android:layout_gravity="bottom" to try to achieve this, I'm unable to see any of the rows that are pushed upwards out of the view of the screen (I can't scroll upwards). However, I'm able to scroll downwards for some reason into emptiness, which shouldn't be possible since the last TableRow should be at the bottom.

Basically the problem is that I can scroll downwards where there are no TableRows but I can't scroll upwards where there are.

This is the relevant XML code:

<ScrollView
    android:id="@+id/tableScrollView"
    android:layout_width="match_parent"
    android:layout_weight="0.7"
    android:layout_height="0dip"
    android:scrollbars="none"
    android:orientation="vertical"
    android:background="@drawable/stripes_background" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="bottom">    

        <TableLayout
            android:id="@+id/outputTable"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:divider="@drawable/table_divider" 
            android:showDividers="middle">    
    </TableLayout>
    </RelativeLayout>

</ScrollView>
Was it helpful?

Solution

It looks like you should be using a ListView or some variant thereof; it provides support for all kinds of optimizations related to scrolling content. Take a look at some of the tutorials and developer docs available for custom ListView implementations.

EDIT Check out the question Listview Scroll to the end of the list after updating the list for details on performing the scroll operation you require using a ListView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top