android – Table with varying column count and auto column widths – Education Career Blog

I want to draw a table/grid by specifying the column count for each row and draw some cells in different colors.
A row should span over the whole screen. The column-widths should be the same for the columns for each row.
I tried using TableLayout with View or Textviews, but I couldn’t figure out how I can distribute the columns evenly for each row.

The grid should look something like this:


I don’t know that a TableLayout is really what you are looking for, since the columns in your different rows don’t line up.

In general, if you want to add a bunch of Views to a row and have them each take up the same amount of space, you can set the android:layout_weight = 1 on each of the views.

For example, for a single row:

<LinearLayout android:layout_width="FILL_PARENT" android:layout_height="WRAP_CONTENT" android:orientation="horizontal">
   <TextView android:layout_weight="1"/>
   <TextView android:layout_weight="1"/>
   <TextView android:layout_weight="1"/>
   <TextView android:layout_weight="1"/>

For the multiple rows, you could use a LinearLayout with android:orientation="vertical", or a TableLayout you could have only one column in your TableRow.

Leave a Comment