Krimson
Krimson

Reputation: 7664

Relative View Align 2 text view, one left align and one right align

Using a relative view, how can I alight 2 text-views in the same line? one has to be stuck to the left and the other onto the right.

If you are a web developer, you can imagin it as applying float:left and float:right to the 2 text-views respectively


Textview 1: Will contain an event title
Textview 2: Will contain the event's time

I want the TextView 2 to be shorter since it will only contain a time whereas TextView1 will contain an event's title

I hope a relative layout is a good approach for this

I want something like this:
enter image description here

Upvotes: 0

Views: 1314

Answers (3)

Kasas
Kasas

Reputation: 1215

You can use a Linear aligned at the top with some weights for each TextView or use a Relative like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_lay"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="left"
        android:text="@string/hello_world" />

</RelativeLayout>

However, I prefer a linear inside a Relative with both texts.

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_lay"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/hello_world" />

</RelativeLayout>

Or

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/hello_world" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

Upvotes: 3

Robby Pond
Robby Pond

Reputation: 73484

If you want the width of each to be equal you can use a LinearLayout.

<LinearLayout android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal">
    <TextView android:layout_height="wrap_content" 
        android:layout_width="0dp" android:layout_weight="1" />
    <TextView android:layout_height="wrap_content" 
        android:layout_width="0dp" android:layout_weight="1" />
</LinearLayout>

Upvotes: 0

Related Questions