0

I have two buttons in a horizontal LinearLayout. They are currently next to each other and the very left. I want to move the second button to the right end of the LinearLayout. I tried android:gravity on these buttons but this didn't change the position of them at all. Thanks

Lara
  • 84
  • 1
  • 7
  • 30

6 Answers6

3

You cannot achieve this using a LinearLayout.
Use a RelativeLayout instead and place each button relative to RelativeLayout right or left. Something like below example:

<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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentRight="true"
        android:text="Button" />

</RelativeLayout>
gunar
  • 14,660
  • 7
  • 56
  • 87
  • 2
    I would be careful saying things like, "You cannot achieve this using...". I obviously agree that using a `RelativeLayout` in this situation is better and easier but it *can* be done with a `LinearLayout` – codeMagic Mar 12 '14 at 17:02
  • @codeMagic: Then please drop an answer with a `LinearLayout` containing only these 2 buttons :) – gunar Mar 12 '14 at 17:11
  • I'm not going to do it because, depending on *exactly* what the OP wants, it would be ridiculous. My point is that it **can** be done so saying that the OP "cannot achieve" this is incorrect. – codeMagic Mar 12 '14 at 17:13
  • Then would you do it for me, please? :) I am curious how ... or at least describe how you would do it. I remember I tried it once and I ended up using RL. Again: a LinearLayout with only 2 buttons inside. – gunar Mar 12 '14 at 17:15
  • Sure, I got nothing better to do (besides work). See my edit. – codeMagic Mar 12 '14 at 17:23
3

The easiest way to do this is to use RelativeLayout. You can give your Button you want on the right the property alignParentRight="true".

<Button
    ...
    android:layout_alignParentRight="true"/>

For a horizontal LinearLayout, android:layout_graivty (which is what you would want instead of android:gravity) left and right won't do anything because the Views are already placed from right to left.

See this answer on the difference between android:gravity and android:layout_gravity if you are uncertain about those.

Edit

Depending on exactly what you need/want, it is possible to do this with a LinearLayout though probably still much easier and more flexible with a RelativeLayout. Anyway, you can use weight to achieve something similar and play with the values. The following gives me a Button on the left and a Button on the right.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Left Button"/>
<View
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="Right Button"/>
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Right Button"/>
</LinearLayout>
Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • @gunar it is still going to place them on the left and right, correct? – codeMagic Mar 12 '14 at 17:24
  • Yes, undoubtedly yes! :) – gunar Mar 12 '14 at 17:25
  • One can very well put a linear layout that has a relative layout with 2 buttons and again: achieved using a LinearLayout :) – gunar Mar 12 '14 at 17:26
  • Also, I already said this is probably not ideal in most cases but I wanted you to stop yelling at me so I made the edit :P The whole point of my comment was to try and help you clarify your answer a little better to make it more accurate. "only siths deal in absolutes" – codeMagic Mar 12 '14 at 17:26
1

Try setting the left button's layout_gravity to left (or start) and the right button's layout_gravity to right (or end).

The problem is that you are currently using gravity which

Specifies how an object should position its content, on both the X and Y axes, within its own bounds.

Instead, you should use layout_gravity that is

Standard gravity constant that a child supplies to its parent. Defines how the child view should be positioned, on both the X and Y axes, within its enclosing layout.

In other words - you are currently telling the buttons how to align their child views, instead of telling them how to be aligned within their parent.

stan0
  • 11,549
  • 6
  • 42
  • 59
0

You can set the android:layout_weight='1' and both buttons will share the screen equally(side by side) or if you want the extra space between them, you can place a button each in a linear layout and set the android:layout_gravity to left and rightfor each.

Sweetie Anang
  • 332
  • 1
  • 3
  • 15
0

Add a RelativeLayout and set values to layout_marginLeft, layout_marginTop, etc.

eg. android:layout_marginLeft="32dp" android:layout_marginTop="160dp"

Curiosity
  • 1,753
  • 3
  • 25
  • 46
0

make linearLayout orientation Vertical and set button's gravity => center and as you want..