Scrolling Table – Part 1

I recently had a requirement to create a table layout which had a header row at the top. On the face of it this is relatively easy using TableLayout, but there was a further requirement: The header row should remain static while the data rows scroll. The problem here is that the standard TableLayout does not support a scrolling body area, so how can we achieve this?

Let’s start by creating a 2.3.3 project named “ScrollingTable” with a package name of “com.stylingandroid.ScrollingTable” and an Activity named “ScrollingTableActivity”.

Let’s add a drawable to res/drawable/border.xml:

Then some dimensions to res/values/dimensions.xml:

And some styles to res/values/styles.xml:

To try and solve this we could create two tables, one outside the ScrollView which contains the header, and the other inside the ScrollView which contains the body:

if we run this, we can see that we get the scrolling behaviour that we require, but the column widths of the body cells do not match those of the header cells:

The column widths problem

The column widths problem

One solution that we could use is to duplicate the entire table, and effectively hide much of it by setting the heights of some of the rows to 0dp. This will effectively hide some of the rows, while maintaining their content which enables TableLayout to correctly calculate the cell widths:

If we run this, we can see that it gives us the scrolling behaviour that we require, and also lines up the columns correctly:

It works!

It works!

Although this does what we require, it is pretty inefficient because we are duplicating the entire table. It works where we have a small number of rows, but is rather hacky, and could run in to memory problems where we have a large number of rows.

After coming up with this approach, I found an article on the Android Adventures blog which offers a similar approach to mine. It is slightly more efficient because it does not fully duplicate the data, but is slightly less flexible as a result because it assumes that the header titles will always be longer than the text in the body cells.

In the second part of this article, we’ll look at an alternative approach which is a little more complex to implement, but should give us the behaviour that we require while being a little better behaved in terms of memory usage.

The source code for this article can be found here.

© 2011, Mark Allison. All rights reserved. This article originally appeared on Styling Android.

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License

Creative Commons License
Scrolling Table – Part 1 by Styling Android, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Terms and conditions beyond the scope of this license may be available at

Tags: ,

6 Responses to “Scrolling Table – Part 1”

  1. Srihari says:

    great work Mark. Thanks

  2. wow why didn’t I know about this blog.

    Starting from helloworld today, will catch u soon.

  3. Par5Eagles says:

    Very nice, works great. Is this type of “scrolloing” solution only possible on android 2.3 & greater? Obviously the code breaks on 2.1 and 2.2…

    • Mark Allison says:

      The only possible reason for the code not working on earlier versions of Android is that we created a 2.3.3 project and haven’t set a “minSdk” attribute in the Manifest. The techniques here are not specific to an particular version of Android, and should be backwardly compatible.

  4. Lorenzo says:

    And if i want to populate cells with child of an xml table?? Thank you you’re a boss!! :)

  5. Fran says:

    The black 3D effect below the header line and the bottom of the main body that appears when scrolling hides the content, how is it made? Does android it automatically?

Leave a Reply