61

Look at the XML code here please:

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- Some stuff goes here -->

    />
    </TableRow>

    <TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- Some stuff goes here -->

    />
    </TableRow>

    <TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <!-- Some stuff goes here -->

    />
    </TableRow>


</TableLayout>

My code is much longer than that but I just eliminated the unnecessary parts. The problem is I want to make this TableLayout a scrollable so that all of my stuff can be shown.

I tried to put this line in the TableLayout in order to make it scrollable:

android:isScrollContainer="true"

But it does NOT do the job. Is there a way ?

Jonik
  • 80,077
  • 70
  • 264
  • 372
iTurki
  • 16,292
  • 20
  • 87
  • 132

4 Answers4

106

Encase the whole thing in:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none"
    android:layout_weight="1">
    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical">

    ...

</ScrollView>
mbauer
  • 348
  • 1
  • 6
  • 25
citizen conn
  • 15,300
  • 3
  • 58
  • 80
  • 5
    @citizen why the `LinearLayout`? – Sudhanshu Jul 14 '14 at 20:12
  • 2
    @Sudhanshu this post is from 3 years ago but as far as I can remember it's the most basic wrapper which sets the orientation to vertical. – citizen conn Aug 14 '14 at 15:22
  • 4
    if you need horizontal scrolling too see http://stackoverflow.com/questions/16623337/how-to-scroll-table-layout-in-horizontal-and-vertical-in-android – wal Sep 30 '15 at 13:34
  • well in my case it is not showing the last few rows (do not scroll till there) – maximus Jun 24 '17 at 14:51
30

You don't technically need the LinearLayout in the ScrollView:

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:layout_weight="1">

    <TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:isScrollContainer="true">

    <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

        <--!Everything Else You Already Have-->

    </TableRow>
 </TableLayout>
</ScrollView>

Once you take up enough room within the ScrollView, the scrolling effect will activate (kind of like an HTML TextArea, once you have enough lines of text, the scrolling activates.)

You can also nest the ScrollView, but again you cannot feel the scrolling effect until you have enough content in the ScrollView.

Artorias2718
  • 575
  • 5
  • 11
  • You're right, inside ConstraintLayout it works too. See my answer bellow. https://stackoverflow.com/questions/6513718/how-to-make-a-scrollable-tablelayout/51178545#51178545 – Nakamoto Jul 04 '18 at 17:22
2

thanks mbauer it's solved my problem i place in order

  1. TableLayout
  2. TableRow with end (for the header of columns)
  3. ScrollView
  4. LinearLayout
  5. x TableRow with end
  6. end LinearLayout
  7. end ScrollView
  8. end TableLayout
Phil
  • 47
  • 1
  • 10
  • 2
    DO NOT WORK! Column width in "2. Table Row" didnt equal to Column width in "5. x TableRow" – AnViKo Jul 17 '17 at 01:11
0

It works too inside Constraint Layout. Just add the following attributes on your TabLayout.

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:isScrollContainer="true">

        . . .
Nakamoto
  • 1,293
  • 14
  • 17