Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Table width and number of Columns in TableLayout in Android

Is there and any method to make table like html in android where i can set row column width as we do in html

<table >
<tr><td style="width:'50%;"></td>
    <td style="width:'50%;"></td>
</tr>
</table>

<TableLayout
           android:id="@+id/tableLayout1"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:stretchColumns="3" >
           <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
           </TableRow>
           <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
           </TableRow>
</TableLayout>

Edit 1:

For something like Html there the percentage width works with respect to its Parent

but the weight introduced in Android works with respect to the screen size available for root.

like image 279
Trikaldarshiii Avatar asked Mar 15 '12 12:03

Trikaldarshiii


People also ask

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.

How do you set column width in a table?

To change the width to a specific measurement, click a cell in the column that you want to resize. On the Layout tab, in the Cell Size group, click in the Table Column Width box, and then specify the options you want.

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.

How can I create a table with borders in Android?

This example demonstrates how create a table with borders in Android . Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

The TableLayout has properties similar to LinearLayout. I did some tweek on it too. To better say it, see my sample code below. Hope its useful.

 <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchColumns="3" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:orientation="horizontal"
            android:layout_weight="1">
            <TextView android:text="@string/test1" android:layout_weight="1"></TextView>
            <TextView android:text="@string/test2" android:layout_weight="1"></TextView>
            <TextView android:text="@string/test3" android:layout_weight="1"></TextView>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:orientation="horizontal"
            android:layout_weight="1">
            <TextView android:text="@string/test1" android:layout_weight="1"></TextView>
            <TextView android:text="@string/test2" android:layout_weight="1"></TextView>
            <TextView android:text="@string/test3" android:layout_weight="1"></TextView>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:orientation="horizontal"
            android:layout_weight="1">
            <TextView android:text="@string/test1" android:layout_weight="1"></TextView>
            <TextView android:text="@string/test2" android:layout_weight="1"></TextView>
            <TextView android:text="@string/test3" android:layout_weight="1"></TextView>
        </TableRow>

    </TableLayout>
like image 88
razzy Avatar answered Sep 20 '22 09:09

razzy