Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place textview on right and left of layout?

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>
like image 613
Sheehan Alam Avatar asked Aug 27 '10 04:08

Sheehan Alam


2 Answers

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>
like image 164
Kevin Coppock Avatar answered Oct 28 '22 02:10

Kevin Coppock


With LinearLayout, just put an empty TextView with the weight between them:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/lefttext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Symbol"/>
   <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
   <TextView
        android:id="@+id/righttext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Price"/>
</LinearLayout>
like image 33
AppiDevo Avatar answered Oct 28 '22 03:10

AppiDevo