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
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
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.
Hello. How to make the first line not scrollable vertically?
Can you tell me how to make the names of the columns of the table always visible?
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
from a printed book, reproduction
Can you please mention the name of the book please ?
can i have the code in java
Code is available in github. just check the java branch.
this is awesome tutorial. The problem in my project is that the onclick listener on the header list item generates an error.
can you please give me the code for implementation
Hi. Here’s the link to the project I created later. Hope it will be helpful for you. Sorry for the delay. I’ll update the post also.
https://github.com/monsterbrain/RecyclerviewTableViewAndroid