Skip to content Skip to sidebar Skip to footer

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 a

Solution 1:

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"?><TableLayoutxmlns: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"><TableRowandroid:layout_height="wrap_content"><TextViewandroid:id="@+id/runLabel"android:text="R"android:layout_height="wrap_content"
             /><TextViewandroid:id="@+id/hitLabel"android:text="H"android:layout_height="wrap_content"
             /><TextViewandroid:id="@+id/errorLabel"android:text="E"android:layout_height="wrap_content"
             /></TableRow><TableRowandroid:layout_height="wrap_content"><TextViewandroid:id="@+id/visitorRuns"android:text="0"android:layout_height="wrap_content"
             /><TextViewandroid:id="@+id/visitorHits"android:text="0"android:layout_height="wrap_content"
             /><TextViewandroid:id="@+id/visitorErrors"android:text="0"android:layout_height="wrap_content"
             /></TableRow><TableRowandroid:layout_height="wrap_content"><TextViewandroid:id="@+id/homeRuns"android:text="0"android:layout_height="wrap_content"
             /><TextViewandroid:id="@+id/homeHits"android:text="0"android:layout_height="wrap_content"
             /><TextViewandroid: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 layoutTableLayouttbl= (TableLayout)findViewById(R.id.RHE);
// delcare a new rowTableRownewRow=newTableRow(this);
// add views to the row
newRow.addView(newTextView(this)); // you would actually want to set properties on this before adding it// add the row to the table layout
tbl.addView(newRow);

Post a Comment for "How To Create A Table In Android With Multiple Columns?"