How to create a table in Android with multiple columns?

68,052

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);
Share:
68,052

Related videos on Youtube

narayanpatra
Author by

narayanpatra

Updated on July 09, 2022

Comments

  • narayanpatra
    narayanpatra almost 2 years

    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)

  • narayanpatra
    narayanpatra about 13 years
    Yes, I want a table layout view.The columns are not properly positioned. How to position the columns? How I can add the rows dynamically?
  • Peter
    Peter about 13 years
    I'm not sure exactly what you mean by 'not properly positioned'. The TableLayout will be sized according to its properties, as are the items inside. Perhaps if you posted your xml as well as what you hope to see, we'd have more ability to answer the question.
  • narayanpatra
    narayanpatra about 13 years
    I used the code of xml file as it is . Just change the first 2 lines. i.e. <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="schemas.android.com/apk/res/android" android:id = "@+id/RHE" It is showing a lot of error. What mistake I am doing here?
  • Peter
    Peter about 13 years
    The style tags will throw you off. I just copied this portion out of one of my working projects, and I didn't notice that it referenced some style.xml attributes. I've taken them out of the example, and it should work fine if you copy/paste it now.