Creating a Table view in Android using Recyclerview

While working on an Android App, there came a requirement to add Table View. I’ve tried several Table View Libraries, but found them too confusing to setup. And when I got a case where the text needs to be fit in, and rows needs to be in equal height when the text grows big, I’ve to rely on RecyclerView to implement my own TableView. So Here it goes.

RecyclerView

Here’s our Recyclerview List Item. The layout height is set to wrap_content for the
maximum content cell, and all others are set to match_parent so when that cell
gets expanded, all other cells also expands.

Table View we are making is a RecyclerView, with a row Item populated. To make the row expanding based on the height of the content of the cell which has the biggest content (height).

Setting up Header And Cells in Adapter

Header has different BG and Color.

In order to setup the cells and header, you need to make some modification in the adapter.
  1. Add a null value as the first item, so that header will be counted.
  2. If Position is zero, setup the viewHolder with the Title String values and set a different background drawable to make it stand out.
  3. If position not zero, fill it with content values, and change the drawable.

Making the Table Scrollable

To make the RecyclerView scrollable horizontally, you can put the RecyclerView inside a horizontal scrollview also by adding property 
<app:layout_behavior=”@string/appbar_scrolling_view_behavior”>

Also when adding to the fragment, adding to the recyclerView the property android:nestedScrollingEnabled=”true”

I’m Planning to make a simple example with GIF Animations, and host it to GitHub. Comments are welcome on any issues that arises.

Update: Code available on Github
https://github.com/monsterbrain/RecyclerviewTableViewAndroid

You may also like...

11 Responses

  1. Gosha says:

    Hello. How to make the first line not scrollable vertically?

  2. Serf says:

    Can you tell me how to make the names of the columns of the table always visible?

  3. Ethan says:

    Hello! I have been trying to add rows dynamically (from a button press) using the cloned GitHub repository, but haven’t been able to successfully implement it yet. Would you know which methods to call in order to get this to work?

    • For that you need to pass your column details as a list to the recyclerView Adapter .. and fill that data with the list.

      Then on button click, you can add data to the list and call the notifyDataChanged function in the adapter

  4. Leupoldkwt says:

    from a printed book, reproduction

  5. delion says:

    can i have the code in java

  6. eric says:

    this is awesome tutorial. The problem in my project is that the onclick listener on the header list item generates an error.

  7. Harsha says:

    can you please give me the code for implementation

Leave a Reply

Your email address will not be published. Required fields are marked *