Skip to content Skip to footer

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

11 Comments

  • Harsha
    Posted April 2, 2019 at 10:28 am

    can you please give me the code for implementation

  • eric
    Posted June 23, 2019 at 11:42 pm

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

  • delion
    Posted April 23, 2020 at 1:02 pm

    can i have the code in java

    • Post Author
      monster Brain
      Posted January 21, 2021 at 12:03 pm

      Code is available in github. just check the java branch.

  • Leupoldkwt
    Posted January 13, 2021 at 6:15 pm

    from a printed book, reproduction

    • Post Author
      monster Brain
      Posted January 21, 2021 at 10:03 am

      Can you please mention the name of the book please ?

  • Ethan
    Posted May 17, 2021 at 11:29 am

    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?

    • Post Author
      monster Brain
      Posted May 18, 2021 at 7:37 am

      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

  • Serf
    Posted July 9, 2022 at 8:20 pm

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

  • Gosha
    Posted July 9, 2022 at 9:38 pm

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

Leave a comment

0.0/5