Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a table in Android with multiple columns?

I want to create a table in android with multiple column. Most of the examples I saw is with 2 columns. (I am new to Java and Android.) I need 3-4 columns and I should be able to add the rows dynamically in the table. Can anyone provide me a sample code. (I am using eclipse in win 7)

like image 585
narayanpatra Avatar asked Mar 08 '11 05:03

narayanpatra


People also ask

What is the table layout in android?

A layout that arranges its children into rows and columns. A TableLayout consists of a number of TableRow objects, each defining a row (actually, you can have other children, which will be explained below). TableLayout containers do not display border lines for their rows, columns, or cells.

Is table layout available in android?

Android TableLayout going to be arranged groups of views into rows and columns. You will use the <TableRow> element to build a row in the table. Each row has zero or more cells; each cell can hold one View object. TableLayout containers do not display border lines for their rows, columns, or cells.

What is stretch column android?

Stretching ColumnsThe specified columns are stretched to take up any available space on the row. Examples: android:stretchColumns="1"—The second column (because the column numbers are 0-based) is stretched to take up any available space in the row.


1 Answers

I assume you're talking about a TableLayout view and not a table in a database??

If so, here's an XML example of a table with three columns and three rows.

Each < TableRow > element creates a row in the table, and each view inside the element creates a "column". I've used TextViews, but they can be ImageViews, EditText, etc.

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id = "@+id/RHE"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="0"
         android:padding="5dp">

     <TableRow android:layout_height="wrap_content">
         <TextView
             android:id="@+id/runLabel"
             android:text="R"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/hitLabel"
             android:text="H"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/errorLabel"
             android:text="E"
             android:layout_height="wrap_content"
             />
     </TableRow>

     <TableRow android:layout_height="wrap_content">
         <TextView
             android:id="@+id/visitorRuns"
             android:text="0"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/visitorHits"
             android:text="0"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/visitorErrors"
             android:text="0"
             android:layout_height="wrap_content"
             />
     </TableRow>

     <TableRow android:layout_height="wrap_content">
         <TextView
             android:id="@+id/homeRuns"
             android:text="0"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/homeHits"
             android:text="0"
             android:layout_height="wrap_content"
             />
         <TextView
             android:id="@+id/homeErrors"
             android:text="0"
             android:layout_height="wrap_content"
             />
     </TableRow>
</TableLayout>

To dynamically change these in the code, you'd have something like this:

// reference the table layout
TableLayout tbl = (TableLayout)findViewById(R.id.RHE);
// delcare a new row
TableRow newRow = new TableRow(this);
// add views to the row
newRow.addView(new TextView(this)); // you would actually want to set properties on this before adding it
// add the row to the table layout
tbl.addView(newRow);
like image 105
Peter Avatar answered Oct 11 '22 20:10

Peter