3

In my Android application there is a screen with some data to be displayed in table format. This table will have around 5 columns and at least 50 rows.

How do I do I create a table in android?

I searched through and everybody seems to recommend to use TableLayout and textView for each cell. Using this technique seems to be a bit difficult as there are lots of textView Components.

Is there any other solution to this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nini
  • 43
  • 5

3 Answers3

3
TableLayout lTableLayout;
lTableLayout = (TableLayout)findViewById(R.id.tblayout);
for(int i=0;i<10;i++){
TableRow tr1 = new TableRow(this);
tr1.setPadding(0, 5, 0, 0);
lTableLayout.addView(tr1, LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
TextView tv11 = new TextView(this);
tv11.setPadding(2, 0, 0, 0);
tv11.setTextSize(12);
tr1.addView(tv11,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); // Can give width and height in numbers also.

}

These will create 10 rows and 1 textview in each row in taken table layout. take one table layout in your xml file like below :

<TableLayout
                    android:id="@+id/tblayout"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >
</TableLayout>
Dhruvisha
  • 2,538
  • 1
  • 21
  • 31
  • Thank you for your answer. I was able to create using the above. But i would like to know how to apply a particular style to the table Row, text view in this case. The style is defined in the styles.xml. – Nini Apr 17 '12 at 11:40
  • http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view May this help u. – Dhruvisha Apr 17 '12 at 11:59
  • Background and Text Color is not an issue. But width of the textview is not getting set when we take the attributes from style or inflate text view. I would like to know why the width alone doesnt work. – Nini Apr 17 '12 at 12:30
  • You can give width to textview by code. No need to use style for that. I have edited my answer for this. Hope that will help you. :) – Dhruvisha Apr 18 '12 at 05:51
  • Thank you. I have done like that only. Wanted to know why width given in the style is not working. Hence asked. – Nini Apr 18 '12 at 09:24
  • ok dear. And if u found your solution pls mark as answered so that others can get answer of same problem. Thank You. – Dhruvisha Apr 18 '12 at 09:45
0

Use ListView with Custom adapter , create five textview's in listview item as columns.. create listitem like this row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
   <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
   <TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
   <TextView android:text="TextView" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
   <TextView android:text="TextView" android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
0

Its not necessary to user TableLayout.

you can user Custom ListView And set header of ListView as your Column name

To create custom listview see this example

MAC
  • 15,799
  • 8
  • 54
  • 95