Skip to content Skip to sidebar Skip to footer

Table With Dynamic Number Of Columns In Android

I want to create a table with dynamic number of columns in android I know how to add rows dynamically using LayoutInflator but how to add columns dynamically? Thank you for your ti

Solution 1:

you dont add columns - only rows and after that you can tell every view how many columns need to span

android:layout_span 

Defines how many columns this child should span. Must be >= 1. 

Must be an integer value, such as "100". 

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type. 

This corresponds to the global attribute resource symbol layout_span.

http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html#attr_android:layout_span

one of the best Tuts: http://mobile.tutsplus.com/tutorials/android/android-sdk_table-layout/


Solution 2:

Use Listview and create a custom layout for row.Use a linear layout with orientation horizontal for row

 android:orientation="horizontal"
 android:weightSum="100" // or some other values

inside this linear layout you can add any number of columns.You can control the width by setting layout weight,You can do this in java dynamically.And make the width of each cell zero and height match_parent.So that your text will be correctly wrapped in each cell.

Setting weight is better than setting width directly since it uses free space and your table will be correctly displayed inside the screen

//Edit

Add a linearlayout in your layout

<LinearLayout 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100" >

Next we have to create columns.Actually it should be like this, here you get 2 columns with half size

<TextView
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
     />
<TextView
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
     />

If you want to do this dynamically you have to write code to create Textview or any other view in java src and set its layout weight as 100/no of columns. You can set weight in layout parameters.


Post a Comment for "Table With Dynamic Number Of Columns In Android"