0

i want to create the table dynamically , now the number of rows and columns are saved in the respective integer variables. please tell me the way i can create the table programatically in android because number of rows and columns are not fixed .

what i tried so far is below...
is there any method to create table with given dimensions(rows and columns)?

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
        TableRow row = new TableRow(this);
        TextView tv = new TextView(this);
        tv.setText("This is text");

        tl.addView(row);
        row.addView(tv);

    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

This is the solution

int yourRowCount = 12;
int yourColumnCount = 12; 


private void createDynamicTable(int yourRowCount) {
    TableLayout tableLayout = new TableLayout(this);

    for (int i = 0; i < yourRowCount; i++) {
        tableLayout.addView(createRow(yourColumnCount));
    }
}


private TableRow createRow(int yourColumnCount) {
    TableRow tableRow = new TableRow(this);
    for (int i = 0; i < yourColumnCount; i++) {
        TextView textView = new TextView(this);
        textView.setText("TextView" + i);
        tableRow.addView(textView);
    }
    return tableRow;
}

just call

createDynamicTable(yourRowCount);