TylerM
TylerM

Reputation: 151

Aligning to left of right in linearlayout

I am trying to set one textview to the left and the other to the right in a horizontal linearlayout, which is nested in a vertical linearlayout nested in a scrollview. The horizontal linearlayout looks like so:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >   
<TextView android:id="@+id/txtType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="Type of Event: "
    style="@style/black_text_bold"
    android:visibility="gone"/>
<TextView android:id="@+id/outputType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="Loading..."
    style="@style/black_text"
    android:visibility="gone"/>    
</LinearLayout>

How would I go about aligning this? I want txttype to start on the left and outputtype to end on the right like:

|                           |
| txtType:       outputType |
|                           |

Thank you in advance,

Tyler

Upvotes: 1

Views: 2697

Answers (5)

Hamid Shatu
Hamid Shatu

Reputation: 9700

In the TextView xml with id android:id="@+id/outputType", change** android:layout_width="wrap_content"** with android:layout_width="0dip" and set android:layout_weight="1". These change will help you to get your result.

<?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="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txtType"
        style="@style/black_text_bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Type of Event: "
        android:visibility="gone" />

    <TextView
        android:id="@+id/outputType"
        style="@style/black_text"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:layout_weight="1"
        android:gravity="right"
        android:text="Loading..."
        android:visibility="gone" />

</LinearLayout>

Upvotes: 1

BigT
BigT

Reputation: 1433

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

    <TextView android:id="@+id/txtType"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="10dip"
       android:layout_width="1"
       android:text="Type of Event: "
       style="@style/black_text_bold"
       android:visibility="gone"/>

    <TextView android:id="@+id/outputType"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="10dip"
       android:layout_width="1"
       android:text="Loading..."
       style="@style/black_text"
       android:visibility="gone"/>    
</LinearLayout>

Upvotes: 1

Piyush
Piyush

Reputation: 18933

Use this..

    <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
         >   
     <TextView android:id="@+id/txtType"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_marginTop="10dip"
       android:layout_weight="1"
       android:text="Type of Event: "
       style="@style/black_text_bold"
       android:visibility="gone"/>

     <TextView android:id="@+id/outputType"
       android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Loading..."
        android:layout_weight="1"
        style="@style/black_text"
        android:visibility="gone"/>    
      </LinearLayout>

Upvotes: 3

Houcine
Houcine

Reputation: 24181

you can just use a RelativeLayout and use attributes alignParentLeft and alignParentRight like this :

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >   
<TextView android:id="@+id/txtType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="10dip"
    android:text="Type of Event: "
    style="@style/black_text_bold"
    android:visibility="gone"/>
<TextView android:id="@+id/outputType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="Loading..."
    android:layout_alignParentRight="true"
    style="@style/black_text"
    android:visibility="gone"/>    
</RelativeLayout>

Upvotes: 2

stan0
stan0

Reputation: 11807

You can use the layout_gravity attribute to set how a child should be positioned in its container.

Your left view's layout_gravity should be left to align it to the container's left edge. And similarly for the right view - layout_gravity right to align it to the container's right edge.

Upvotes: 1

Related Questions