ury
ury

Reputation: 1160

Aligning text cells in a TableLayout

How can I align text content in a two-column TableLayout so that the text in the left column in aligned to the right and the text in the left column is aligned to the left, making them "fastened" to each other around the middle of the screen?

It should look something like this:

+-----------------------------------+
|           Title: | Value          |
+-----------------------------------+

Upvotes: 1

Views: 4489

Answers (2)

Leandroid
Leandroid

Reputation: 2047

Here is a small example that provides what you want

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">
        <TextView
            android:text="left at right"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:paddingRight="5dp" />
        <TextView
            android:text="right at left"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:paddingLeft="5dp" />
    </TableRow>
</TableLayout>

Upvotes: 5

Gautam
Gautam

Reputation: 7958

Assuming you are using TextView

Use the attributes

<TextView
  ....
  android:gravity="center|right"` 
/>

,

<TextView
  ....
  android:gravity="center|left"
/>

in the xml layout

or

textView.setGravity(Gravity.RIGHT|Gravity.CENTER); and textView.setGravity(Gravity.LEFT|Gravity.CENTER); in java

Upvotes: 3

Related Questions