2

I have a TableLayout contained in a Scrollview. The TableLayout is populated programatically during runtime. The table contains flight arrival information. One the TableLayout has been populated, I want to be able to scroll down to the current time period.

If I know that I want to scroll to row number 40 of 100 in the table, how can I do this?

Here is the XML of the layout. Thanks in advance.

<LinearLayout
        android1:id="@+id/LinearLayout20"
        android1:layout_width="fill_parent"
        android1:layout_weight="1"
        android:orientation="vertical"
        android1:background="#000000" android:layout_height="10000dp" android:measureWithLargestChild="true">

    <ScrollView
            android1:id="@+id/scrollView1"
            android1:layout_width="match_parent"
            android1:layout_height="wrap_content">

        <TableLayout
                android1:id="@+id/arrivalstable"
                android1:layout_width="wrap_content"
                android1:layout_height="wrap_content">
        </TableLayout>
    </ScrollView>

</LinearLayout>
user2608461
  • 21
  • 1
  • 4
  • Possible duplicate of [Is there a way to programmatically scroll a scroll view to a specific edit text?](http://stackoverflow.com/questions/6831671/is-there-a-way-to-programmatically-scroll-a-scroll-view-to-a-specific-edit-text) – José Sep 22 '16 at 20:27

2 Answers2

6

This might be a late answer, but hopefully it can help someone. If you more closely follow Randy's link, you'll see you need to place the scrollview in a runnable, otherwise child.getTop() will always produce 0.

final ScrollView sv = (ScrollView) findViewById(R.id.scrollView1);
TableLayout tl = (TableLayout) findViewById(R.id.arrivalstable);
int index = 40;
final View child = tl.getChildAt(index);
new Handler().post(new Runnable() {
    @Override
    public void run() {
        sv.smoothScrollTo(0, child.getBottom());
    }
});
Community
  • 1
  • 1
buczek
  • 2,011
  • 7
  • 29
  • 40
0

Take a look here: Is there a way to programmatically scroll a scroll view to a specific edit text?

int index = 40;
View child = mytablelayout.getChildAt(index);
myscrollview.scrollTo(0, child.getTop());

Basically you just need to get the position of the view and tell the scrollview to scroll to it. This requires you to have a reference to the item you want to scroll to but you can also get that dynamically.

Community
  • 1
  • 1
Randy
  • 4,351
  • 2
  • 25
  • 46
  • When I try this, the value for child.getTop() is Zero. there are over 100 rows in the table, so it should find it. Also, when I do myscrollview.getHeight(), it returns zero also. Once the table has been populated, is there a delay before the heights would be reported? The table is being populated on the onPostExecute method of an AsyncTask. – user2608461 Aug 29 '13 at 09:28
  • @user2608461 - Just speculating, but it may have something to do with the layout_height of your LinearLayout being 10000dp. – Randy Aug 30 '13 at 01:22
  • My Layout wasn't filling the screen, I read somewhere that setting the Layout height to a large dp value, would sort this. – user2608461 Sep 03 '13 at 00:17