Creating table layout dynamically in android

In first(Creating table layout from xml layout) , table layout creating with xml layout, Now creating table layout from programmatically in android.

public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
   String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6",
"Row 7" };
        String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4",
  int rl=row.length; int cl=column.length;
   Log.d("--", "R-Lenght--"+rl+"   "+"C-Lenght--"+cl);
ScrollView sv = new ScrollView(this);
   TableLayout tableLayout = createTableLayout(row, column,rl, cl);
   HorizontalScrollView hsv = new HorizontalScrollView(this);


public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
   // get row from table with rowIndex
   TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);

   // get cell from row with columnIndex
   TextView textView = (TextView)tableRow.getChildAt(columnIndex);

   // make it black
public void setHeaderTitle(TableLayout tableLayout, int rowIndex, int columnIndex){
   // get row from table with rowIndex
   TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);

   // get cell from row with columnIndex
   TextView textView = (TextView)tableRow.getChildAt(columnIndex);

private TableLayout createTableLayout(String [] rv, String [] cv,int rowCount, int columnCount) {
   // 1) Create a tableLayout and its params
   TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
   TableLayout tableLayout = new TableLayout(this);

   // 2) create tableRow params
   TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
   tableRowParams.setMargins(1, 1, 1, 1);
   tableRowParams.weight = 1;

   for (int i = 0; i < rowCount; i++) {
       // 3) create tableRow
       TableRow tableRow = new TableRow(this);

       for (int j= 0; j < columnCount; j++) {
           // 4) create textView
           TextView textView = new TextView(this);
         //  textView.setText(String.valueOf(j));
           String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
Log.d("TAG", "-___>"+id);
            if (i ==0 && j==0){
            } else if(i==0){
            Log.d("TAAG", "set Column Headers");
            }else if( j==0){
            Log.d("TAAG", "Set Row Headers");
            }else {
            // check id=23

           // 5) add textView to tableRow
           tableRow.addView(textView, tableRowParams);

       // 6) add tableRow to tableLayout
       tableLayout.addView(tableRow, tableLayoutParams);

   return tableLayout;

Out put looks like this:
Happy Coding!!!


  1. This comment has been removed by the author.

  2. Many Thanks for posting the article. It worked like a charm even on the latest Android Studio 1.5.1. Just couple of changes required to display the last row and the last column.

    The for loop to be changed to 'less than equal to rowcount' (int i = 0; i <= rowCount; i++)', same for the loop for the column 'for (int j= 0; j <= columnCount; j++)'


  3. 0
    down vote
    table dynamic as I prepare my lines and I want to get the value entered in the column . I appreciate your help . I'm pulling data SQLite. I create the TableLayout in tabloyu . I add a picture in the picture edittex s 8 by 5 and you want to get the value



Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace