Emanuil Rusev
Emanuil Rusev

Reputation: 35235

How do you put two Views in a horizontal Layout so that one is aligned to the left and the other one - to the right?

Let's say you want to have a TextView and a Button in a Layout with horizontal orientation. Can you have the TextView aligned to the left and the Button - to the right?

---------------------
TextView       Button
---------------------

Upvotes: 2

Views: 1055

Answers (2)

TWH
TWH

Reputation: 71

1) Use a RelativeLayout and android:layout_alignParentLeft="true" on one and android:layout_alignParentRight="true" on the other.

2) Use a LinearLayout with android:orientation="horizontal", and set android:gravity="left" & android:layout_weight="1" on one and android:gravity="right" & android:layout_weight="1" on the other (yes, the weight is required!)

Note, using layout_toRightOf will give left/right relative positioning but it won't force them to the edge of the screen/parent container.

Upvotes: 4

ktingle
ktingle

Reputation: 374

Use a relative layout and position the second view to right.

android:layout_toRightOf="@id/firstView"
android:layout_alignTop="@id/firstView"

Upvotes: 3

Related Questions