0

I have a LinearLayout with 2 buttons splitting it horizontally.

I want those buttons to be at the bottom of the screen, so I added to the layout android:layout_weight="bottom" and nothing changes!

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Start" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Stop" />
    </LinearLayout>

</LinearLayout>
double-beep
  • 5,031
  • 17
  • 33
  • 41
aclowkay
  • 3,577
  • 5
  • 35
  • 66

4 Answers4

1

You need to set the layout_width of your buttons to 0dp.

Additionally there is no width property:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
android:layout_alignParentBottom="true" >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Start" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Stop" />
</LinearLayout>

</RelativeLayout>
double-beep
  • 5,031
  • 17
  • 33
  • 41
David Hirst
  • 1,890
  • 2
  • 22
  • 31
  • I changed it the buttons' width to 0dp, and yeah I dont know why I put the width property.. and it still doesn't change anything.. – aclowkay Jan 09 '13 at 17:00
  • You are using the wrong attribute for the parent layout. It should be android:gravity="bottom" if you want the child object to sit at the bottom of its parent container (in this case the linearlayout), you have used layout_gravity, which is a child attribute. – David Hirst Jan 09 '13 at 17:04
  • The linear layout is a child of the main linear layout . I want the whole (child)layout to be at the bottom – aclowkay Jan 09 '13 at 17:06
  • Your original code does not show a child linear layout. You have a single parent layout with two buttons, where is the other layout? If this is not your complete layout please update your post with the full layout file. – David Hirst Jan 09 '13 at 17:08
  • I'm glad this resolved your issue, please mark it as the answer so it can help others :) – David Hirst Jan 10 '13 at 09:07
  • I did, and, is there a solution with linear layout? because when I used it in another layout, there are new problems of relative layouts like one element overlapping another.. – aclowkay Jan 11 '13 at 15:00
  • If you want to dock elements you cannot use LinearLayout, you need to use RelativeLayout. In addition if you want to evenly space elements you need to use LinearLayout because it has the layout_weight property which inversely RelativeLayout does not have. You could get so far just using LinearLayout and mix of margin properties etc but you would never guarantee your elements would sit at the bottom of the screen for all devices. Hence you use the approach above. – David Hirst Jan 14 '13 at 14:57
1

The layout_weight is used to divide up remaining space after layout_width or layout_height have been taken into account.

When you say

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="Start"
    android:width="0dp" />

The android:layout_width="fill_parent" says to set the size of the button to as wide as its parent. Doing so for two buttons in a horizontal linear layout will result in only one visible button; the second is actually positioned to the right of the first, but offscreen.

If you set the layout_width to 0dp for both buttons, both buttons will have the same width.

If you set the layout_width to wrap_content on both buttons, both will start at their "preferred" width and then have additional space allocated to them based on the ratio of their layout_width to the sum of the layout_widths of all views in the parent layout.

If you want the buttons at the bottom, you can either change your layout to a relative layout (and use alignParentBottom as mentioned in other answers), or nest inside a vertical linear layout (the horizontal layout would have layout_height='wrap_content' and layout_width='fill_parent').

Because you mentioned nesting, you could use something like

<LinearLayout
    andrdoid:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ...
    >
        <LinearLayout
            anddoid:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            ...
            >
                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    ... />
                <Button
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    ... />
        </LinearLayout>
</LinearLayout>

(or use wrap_content for the layout_width of the buttons to start them at their text widths)

Scott Stanchfield
  • 29,742
  • 9
  • 47
  • 65
0

change the layout_width param on the buttons to

android:layout_width="0dp"

Also you can use layout_alignParentBottom

android:layout_alignParentBottom="true"

You can create your layout file like following

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

  <!-- Other Components of your layout file -->
  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom"
  android:layout_alignParentBottom="true"
  android:orientation="horizontal"
  android:weightSum="2">
    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Start"/>

    <Button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Stop"/>
  </LinearLayout>
</RelativeLayout>
Meghal Shah
  • 405
  • 2
  • 11
0

Your title says layout_weight isn't working for you, but the question body implies it is working just fine. The layout_weight attribute should be causing your buttons to take up half the width each.

The positioning at the bottom of the screen is a different issue. Based on your recently edited xml, add android:gravity="bottom" to the parent LinearLayout, or android:layout_gravity="bottom" to the child LinearLayout.

dokkaebi
  • 9,004
  • 3
  • 42
  • 60