views:

133

answers:

3

I have a relative layout which looks like this: alt text

Here is the code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >   
    <TextView
        android:id="@+id/nameText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:layout_alignParentLeft="true"
        android:text="Symbol"
        />
    <TextView
        android:id="@+id/priceText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:layout_toRightOf="@+id/nameText"
        android:gravity="right"     
        android:text="100"
     />  
    <TextView
        android:id="@+id/changeText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:layout_toRightOf="@+id/priceText"
        android:gravity="right"     
        android:text="1.3"
        />  
</RelativeLayout>

How can I get the company name to line up next to the number 1.3?

A: 

It's not clear exactly what you want to have happen, but here are a few options:

If you want columns to be aligned across rows, you might consider using a TableLayout.

You can also use the "weight" atribute to affect what percentage of the screen each TextView takes up. If you assign a value of 1 on the left-most text view, and 0 on the other 2, this will cause the left text view to take up all the extra space, and thus push the middle text view to the right. This will probably cause the middle text view to look right aligned though. You could instead give the left one 20%, the middle one 50% and the right one 30% by assign 2, 5 and 3.

You could also just set an explicit size for the text views (in dpi), but this might be problamatic with different sized screens.

Mayra
yes, I want the company name to be right aligned next to the price
Sheehan Alam
can you show in code, how to do the table layout? i inserted a table row and now the textviews are stacking on-top of each other vertically instead of horizontally.
Sheehan Alam
See hello tableLayout: http://developer.android.com/guide/tutorials/views/hello-tablelayout.html There are good tutorials for pretty much every layout or view type.
Mayra
A: 

I agree with Mayra! Use the TableView, you'll get the formatting your looking for as well as keep almost all functionality of a ListView (scolling, and list oriented functionality)

A: 

Following up on Mayra's answer (as promised, haha!) here's one way to do it using layout_weight:

<?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>  
        <TextView
            android:id="@+id/left_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left|center_vertical"
            android:padding="5dip"
            android:layout_weight="0"
            android:text="Code"
            />
        <TextView
            android:id="@+id/middle_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right|center_vertical"
            android:padding="5dip"
            android:layout_weight="1"
            android:text="Name of Company"
            />      
        <TextView
            android:id="@+id/right_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip"
            android:gravity="right|center_vertical"
            android:layout_weight="0"       
            android:text="1.3"
            />
    </TableRow>
</TableLayout>
kcoppock
as always, rock solid answer! does having a tablelayout instead of a relative layout affect performance since its in a listview?
Sheehan Alam
Hmm, I'm afraid I can't really help too much on that front. I'm not sure how that would affect performance. I wouldn't THINK it would cause much of a difference.
kcoppock