views:

76

answers:

2

I want to place a textview on the right of a layout and on the left of a layout, but they keep stacking ontop of each other:

<LinearLayout
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent" android:orientation="horizontal">
        <TextView
            android:id="@+id/lefttext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:text="Symbol"/>
       <TextView
            android:id="@+id/righttext"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:text="Price"/>
    </LinearLayout>
+1  A: 

Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/left_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Symbol"
        />
    <TextView
        android:id="@+id/right_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Price"
        />
</LinearLayout>

Why are you specifying 0dip for the height on these?

For symbols on sides:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/left_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dip"
        android:gravity="left"
        android:text="Symbol"
        />
    <TextView
        android:id="@+id/right_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dip"
        android:gravity="right"
        android:text="Price"
        />
</LinearLayout>

And done with a RelativeLayout:

<?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/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:layout_alignParentLeft="true"
        android:text="Code"
        />
    <TextView
        android:id="@+id/right_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:layout_toRightOf="@+id/left_text"
        android:gravity="right"     
        android:text="Company Name"
        />  
</RelativeLayout>
kcoppock
any way to make symbol be on the far left and price be on the far right?
Sheehan Alam
Yes, just adjust the gravity setting to whatever you would like. I've added an example above. (I also added a small amount of padding, so that they aren't pressed up against the sides)
kcoppock
The right_text doesn't seem to gravitate towards the right, check out my screenshot: http://cl.ly/786ff7d72c99537d190b
Sheehan Alam
Are you using this in a ListView?
kcoppock
Yes I am using this in a ListView
Sheehan Alam
Try it like the one I just added. A RelativeLayout might work better for you here.
kcoppock
that looks more like it! how can I specify the height of each row to be larger?
Sheehan Alam
Hmm, I can't remember the elegant way right now...but you should be able to add android:paddingTop="some number" and android:paddingBottom="some number" to your RelativeLayout, which should do the trick, I believe.
kcoppock
thanks for the help! I posted a new question, because I added in another column, but am having trouble aligning it: http://stackoverflow.com/questions/3586235/how-to-create-column-like-layout-for-listview-rows
Sheehan Alam
I'll try to answer that later if no one has answered it yet. (I'm at work, no Eclipse here for me. :( )Just from a quick look, though, from your comments. Each TableRow will make a new "line", but the row can contain multiple columns by adding Views. So <TableRow><TextView/><TextView/></TableRow>, for example.
kcoppock
A: 

Use android:layout_alignParentLeft="true" and android:layout_alignParentRight="true" on view inside RelaytiveLayout like this

<?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="wrap_content"
    >
    <TextView
        android:id="@+id/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="left|center_vertical"
        android:text="Symbol"
        />
    <TextView
        android:id="@+id/right_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:gravity="right|center_vertical"
        android:text="Price"
        />
</RelativeLayout>

Alternatively you can use TableLayout with stretchColumn set to stretch the second column to the size of parent.

RobGThai