Jennifer
Jennifer

Reputation: 1852

How to align the texts in a layout in android

I want to display some text in a ListView row with the following alignment:

Mike      10pt $1,233,500
Mike2    200pt $1,2,500
Holly     10pt $4500

But when I tried, I got this instead:

   Mike        10pt   $1,233,500
    Mike2    200pt       $12,500
    Holly            10pt  $4500

My layout file is below. What am I doing wrong?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:background="@drawable/dl005_textfield_selected"
        android:paddingBottom="5dp" >

        <TextView
            android:id="@+id/Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Mike"
            android:layout_weight="1"
            android:textColor="#888888" />

        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:gravity="right"
            android:text="10pt"
            android:textColor="#3498db" />

        <TextView
            android:id="@+id/myid"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="$1,233,500"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_weight="1"
            android:gravity="right"
            android:textColor="#3498db" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

Upvotes: 2

Views: 327

Answers (1)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

You should be using a TableLayout instead which positions its children views into rows and columns. Also note that you need to remove android:gravity="right" from the third TexView because you need its contents left aligned.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1" >

    <TableRow
        android:background="@drawable/dl005_textfield_selected"
        android:paddingBottom="5dp" >

        <TextView
            android:id="@+id/Name"
            android:layout_marginLeft="10dp"
            android:text="Mike"
            android:textColor="#888888" />

        <TextView
            android:id="@+id/score"
            android:padding="10dp"
            android:gravity="right"
            android:text="10pt"
            android:gravity="right"
            android:textColor="#3498db" />

        <TextView
            android:id="@+id/myid"
            android:text="$1,233,500"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="#3498db" />

    </TableRow>

    ...

</TableLayout>

You also need to remove the layout_width attributes on child TextViews as they are always set to MATCH_PARENT. You set the individual columns as strechable or shrinkable instead.

Upvotes: 5

Related Questions