Creating tables – TableLayout and GridLayout
When you need to create a table in your UI, Android provides two convenient layout options: the TableLayout
(along with TableRow
) and the GridLayout
(added in API 14). Both layout options can create similar looking tables, but each using a different approach. With the TableLayout
, rows and columns are added dynamically as you build the table. With the GridLayout
, row and column sizes are defined in the layout definition.
Neither layout is better, it's just a matter of using the best layout for your needs. We'll create a 3 x 3 grid using each layout to give a comparison, as you could easily find yourself using both layouts, even within the same application.
Getting ready
To stay focused on the layouts and offer an easier comparison, we will create two separate applications for this recipe. Create two new Android projects, the first called TableLayout
and the other called GridLayout
.
How to do it...
- Starting with the
TableLayout
project...