Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set android table layout Column padding in xml

Wireframe of my layout

I managed to design the layout using TableLayout. android:layout_span helped me on that.

The problem is I need some gap between Column1 and Column2. In either way Padding / Margin

I am not prefering program for this task. there must be a simple way to done this.

Is that possible to set using xml?

Update: I tried to set column child's padding It's not align the columns evenly.

like image 686
Mahendran Avatar asked Jul 11 '12 14:07

Mahendran


People also ask

How do I padding in XML?

android:paddingRight=”size in dp” android:paddingLeft=”size in dp” android:paddingTop=”size in dp” android:paddingBottom=”size in dp”

What is stretch column android?

Stretching ColumnsThe specified columns are stretched to take up any available space on the row. Examples: android:stretchColumns="1"—The second column (because the column numbers are 0-based) is stretched to take up any available space in the row.

What is the use of table layout in Android?

TableLayout is a ViewGroup that displays child View elements in rows and columns. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout. TableLayout positions its children into rows and columns.


1 Answers

Try adding android:layout_marginRight="8dp" the View in the column to the left of the place where you want the gap to be for each row in layout XML. It worked for me.

Better yet, you could put the padding dimension in the values/dimens.xml file and reference the margin from there.

For example:

values/dimens.xml

<resources>
    <dimen name="column_1_right_margin">8dp</dimen>
</resources>

layout/table.xml

<TableLayout
    ... layout params>
    <TableRow>
        <TextView
            ... layout params
            android:layout_marginRight="@dimen/column_1_right_margin" />
        <TextView
            ... layout params />
    </TableRow>
    <TableRow>
        <TextView
            ... layout params
            android:layout_marginRight="@dimen/column_1_right_margin" />
        <TextView
            ... layout params />
    </TableRow>
</TableLayout>

To clear it up even more and stop code duplication, you could make a style for column 1 then all you'd have to do is apply the style for each View in the first column. See here for more info.

like image 152
Tim Kist Avatar answered Sep 18 '22 17:09

Tim Kist