I m trying to have a textview and a button in linear layout with horizontal orientation. The textview should appear at the starting and the button should appear at the end. I thought giving gravity right to the button would do the trick but the buttons doesn't move to the right side. I m thinking if I should probably use relative layout?
<\LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs 3579.0"
/>
<Button
android:id="@+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Buy Now" />
<\/LinearLayout>
My way (using a RelativeLayout):
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Rs 3579.0"
/>
<Button
android:id="@+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Buy Now"
/>
</RelativeLayout>
See how I explicitly align the TextView to the Parent's left side and the Button to the Parent's right side
You can then center the TextView vertically in the RelativeLayout, by setting:
android:layout_centerVertical="true"
in the TextView itself
Newr OS versions may prefer this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Rs 3579.0"
/>
<Button
android:id="@+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="Buy Now"
/>
</RelativeLayout>