Showing posts with label TableLayout. Show all posts
Showing posts with label TableLayout. Show all posts

Wednesday, June 29, 2011

How to programmatically add cells (Views) and rows in TableLayout

 Following are the steps for programmatically adding rows in a TableLayout:

1. Manually include a TableLayout in your main.xml file with an id, say, "TableLayout01". Obtain a reference to the TableLayout as:
       
    table = (TableLayout) findViewById(R.id.TableLayout01); 

Note: Ensure to define the variable "table" outside onCreate() inside the class as: TableLayout table;

How to display grids on TableLayouts

Android TableLayouts apparently do not have an option to show grids on the tables. In the following example, grid-like appearance is creates by setting margins around each TextView inside the table, and setting different background colors for the TableLayout and the TextViews.