How to create a table view with multiple columns in android using java code?

I am developing an Android application. For this I need to have a dynamic tableView. I am using TableLayout for what is available in android. But I could not find a way to have multiple columns in my TableView. Any option please?

+3
source share
2 answers

I don't know if I fully understand your question, but here:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    TableLayout tableLayout = new TableLayout(getApplicationContext());
    TableRow tableRow;
    TextView textView;

    for (int i = 0; i < 4; i++) {
        tableRow = new TableRow(getApplicationContext());
        for (int j = 0; j < 3; j++) {
            textView = new TextView(getApplicationContext());
            textView.setText("test");
            textView.setPadding(20, 20, 20, 20);
            tableRow.addView(textView);
        }
        tableLayout.addView(tableRow);
    }
    setContentView(tableLayout);
}

This code creates a TableLayout with 3 columns and 4 rows. Basically you can declare a TableLayout in an XML file, then setContentView in XML and use findViewById to search for a TableLayout. Only TableRow and its children should be executed in Java code.

+16
source

, . , , View, TablaRow .

<TableLayout>
  <TableRow>
    <TextView></TextView> // That a column
    <ImageView></ImageView>  // That other column
    ....

    <Other views></Other views> // That the last column
  </TableRow>
 </TableLayout>
0

All Articles