LinearLayout puts the child in the desired direction
I am trying to get a text image and a button in a linear horizontal layout. The text view should appear at startup and the button should appear at the end. I thought that giving gravity to the button would do the trick, but the buttons won't move to the right. I think 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>
source to share
My way (using 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 will explicitly align the TextView to the left side of the Parent and the Button to the right side of the Parent
Then you can center the TextView vertically in the RelativeLayout by setting:
android:layout_centerVertical="true"
in the TextView itself
source to share
Try below xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/productPriceTextView1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Rs 3579.0"
/>
<Button
android:id="@+id/buyNowButton1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Buy Now" />
</LinearLayout>
source to share
There is a cleaner way to do this using LinearLayout: just give the left element a width of 0 and a weight of 1, and set the correct width to wrap_content. What is it!
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/productPriceTextView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rs 3579.0"
/>
<Button
android:id="@+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Now" />
</LinearLayout>
source to share
There are two approaches:
1) You can use RelativeLayout
which you can drag Button
to wherever you want.
2) You can use the weight property for LinearLayout
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/productPriceTextView1"
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Rs 3579.0"
/>
<Button
android:id="@+id/buyNowButton1"
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Buy Now" />
</LinearLayout>
source to share
Use this layout instead.
<RelativeLayout 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:id="@+id/productPriceTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs 3579.0"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Now"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
source to share
Another simple solution that doesn't rely on usage RelativeLayout
is to have a blank view
between two elements, using layout_weight
to make sure it fills up the blank space.
<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"/>
<view
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/buyNowButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Buy Now" />
</LinearLayout>
This way you can achieve what you want using only LinearLayout
, now I'm not sure if this is more efficient than using RelativeLayout
. But for me it always feels a bit like an unnecessary move for a relative in such a simple way.
source to share