7

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?

enter image description here

<\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>
Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126

8 Answers8

13

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>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Thanks this worked for me. @FrankNStein please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 02 '14 at 03:22
8

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>
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
7

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 right one's width on wrap_content. And that's 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>
VGM
  • 253
  • 4
  • 8
1

There are two approach:

1) You can use RelativeLayout in which you can drag your Button where you want..

2) You can use 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>
Piyush
  • 18,895
  • 5
  • 32
  • 63
1

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>
williamj949
  • 11,166
  • 8
  • 37
  • 51
0

use android:layout_gravity="end" to your linear layout to move all the elements to the right

Abhilash Das
  • 1,388
  • 1
  • 16
  • 22
0

Another simple solution here that doesn't rely on using a RelativeLayout is having an empty view between both elements using layout_weight to make sure it fills the empty 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>

That way you can achieve what you want by only using a LinearLayout, now, I am not sure if this is more efficient than using a RelativeLayout. But for me it always feels a bit like an overkill to go for a relative in such a simple layout.

Raeglan
  • 514
  • 1
  • 5
  • 17
0

Just add empty view with 0dp width and 1 weight

<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_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/buyNowButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:text="Buy Now" />
</LinearLayout>
Arie Agung
  • 108
  • 1
  • 8