2

I have a TableLayout which is populated dynamically with many rows of data. It contains more data than the screen can hold, so I require it to be scrollable.

My issue is that when I place the TableLayout inside of a ScrollView, it appears to cut off many rows from the top of the TableLayout.

For reference, the full code is here (the question is to cluttered already to fit it all) http://pastebin.com/w7Fi3Bzz

See below for the code related to the issue I'm having

Here is the XML without the ScrollView:

        <TableLayout
            android:orientation="vertical"
            android:fillViewport="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:id="@+id/tbl_statistics"
            android:stretchColumns="*"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            >
        </TableLayout>

And what it looks like:

enter image description here

And here is the same XML with an enclosing ScrollView:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    >

        <TableLayout
            android:orientation="vertical"
            android:fillViewport="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:id="@+id/tbl_statistics"
            android:stretchColumns="*"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            >
        </TableLayout>

</ScrollView>

And here is what the table looks like, you can see the top of the table was truncated (it is populated with the same exact data):

enter image description here

I found a similar issue here LinearLayout not expanding inside a ScrollView, but the answers did not help me.

Community
  • 1
  • 1
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • You don't need the `LinearLayout` in your second layout. – Mike M. Apr 26 '16 at 03:29
  • @MikeM. This issue persists whether I include it or not – James Wierzba Apr 26 '16 at 03:29
  • I second @MikeM. the `LinearLayout` is redundant. You should make `layout_height` of `TableLayout` to `wrap_content` when you remove the `LinearLayout` – Abdullah Apr 26 '16 at 03:29
  • Try removing the `layout_gravity` attribute in the `TableLayout`, without the `LinearLayout`. Also, the `fillViewport` is useless on anything other than a `ScrollView`. – Mike M. Apr 26 '16 at 03:31
  • Alright guys I will remove it to simplfy my question but I've tried it both ways and it still truncates – James Wierzba Apr 26 '16 at 03:32
  • @MikeM. I've tried removing the layout_gravity tag (and I've also removed the LinearLayout). I still get the same results – James Wierzba Apr 26 '16 at 03:35
  • I would guess, then, that the top of the `ScrollView` is under another `View`, as you've got its `layout_height` at `fill_parent`. – Mike M. Apr 26 '16 at 03:38
  • @MikeM. Yes, it is enclosed within a LinearLayout for the activity shown in this app, along with the Spinners and Button you can see in the image. Would it be useful to add that? The question already has so much code and images. – James Wierzba Apr 26 '16 at 03:40
  • Well, before posting the whole thing, try setting the `ScrollView`'s `layout_height` to `0dp`, and its `layout_weight` to `1`. – Mike M. Apr 26 '16 at 03:41
  • @MikeM. No luck trying that. I've also edited my answer to include the full code for my activity layout. thanks for your help so far – James Wierzba Apr 26 '16 at 04:21
  • Tested and verified again. If that doesn't solve your problem, then you'll need to post the code that's generating the table, and any code that might modify the `TableLayout`'s `LayoutParams`. Really, the crux of your problem is the `TableLayout`'s `layout_gravity` attribute. – Mike M. Apr 29 '16 at 02:21

2 Answers2

3

In your current layout:

  1. Remove android:layout_gravity="center" from the TableLayout element.

  2. Add android:layout_weight="1" to the ScrollView element.

  3. Clean and rebuild your project.

Additionally, the fillViewport attribute only works on ScrollViews.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • That did it. Thank you. – James Wierzba Apr 29 '16 at 02:45
  • I will award my bounty I first want to deploy my application and verify it works in production. Thank you for all the help over the last 2 days. – James Wierzba Apr 29 '16 at 02:46
  • 1
    No problem! Sorry you had to spend 50 rep. I'd've come back to help for nothin'. :-) Just happened to see your question in the bounties. Hope it finally fixes it for ya everywhere. Cheers! – Mike M. Apr 29 '16 at 02:50
2

I resolved the same issue only by inserting TableLayout into LinearLayout which is only child of ScrollView. I tried all other variants without success (fillViewport for ScrollView, android:layout_weight="1" for TableLayout).

I used TableLayout to dynamically insert Views to it

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TableLayout
            android:id="@+id/activity_displayFields_tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp" />
    </LinearLayout>
</ScrollView>
papandreus
  • 314
  • 3
  • 12