Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Two buttons in same line filling the whole width

I have a little problem defining a Relative Layout. I have a List View with scroll and two buttons always visible at the bottom of the list view. I just would like my two button have 50% of the width, filling the line. This is my code:

<?xml version="1.0" encoding="utf-8"?>

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

    <Button 
        android:id="@+id/testbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Save" />

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" 
        android:layout_toRightOf="@+id/testbutton"
        android:text="Cancel"/>

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/LstPeriodOptions"
        android:layout_alignParentTop="true" 
        android:layout_above="@id/testbutton" />

</RelativeLayout>

I tried to introduce the buttons in a Linear Layout and give the gravity=1 with width=0dp but in that case the ListView dissapears. Could you help me please?

Sorry for my english. This is the result I would like to have:

Result

Thanks a lot, best regards.

EDIT: This is what I tried with Linear Layout:

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/container" >

        <Button 
            android:id="@+id/testbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Guardar" />

        <Button
            android:id="@+id/cancelButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" 
            android:layout_toRightOf="@+id/testbutton"
            android:text="Cancelar"/>
    </LinearLayout>

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/LstPeriodOptions"
        android:layout_alignParentTop="true" 
        android:layout_above="@id/container" />

</RelativeLayout>
like image 525
javifm Avatar asked Oct 02 '13 12:10

javifm


People also ask

Is it possible to evenly distribute buttons across the width of an android LinearLayout?

I suggest you use LinearLayout's weightSum attribute. Adding the tag android:weightSum="3" to your LinearLayout's xml declaration and then android:layout_weight="1" to your Buttons will result in the 3 buttons being evenly distributed.

How do you put two buttons on the same line in linear layout?

If you are placing the buttons inside the LinearLayout, give the Orientation value as "Vertical", it will automatically place the buttons in the same line. If you are using RelativeLayout, then for one button use android:layout_toLeftOf OR android:layout_toRightOf and give value as ID of other button.

How do you put a space between two buttons horizontally on Android?

The best is to use android:layout_marginTop="10dp" in your XML activity as this gives accurate spacing between that button and the other button or widget. Repeat this for rest of the buttons.

How do you put a space between buttons in linear layout?

good answer. Small improvement: use a <Space android:layout_width="0dp" android:layout_height="1dp" android:layout_weight="1" > </Space> instead of a <View> to make your objective clearer in your XML.


2 Answers

Did you try with your LinearLayout in this way because this should work. Note all of the property changes. Since I don't know how yours was, I can't point out all of the differences.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
   <LinearLayout
      android:id="@+id/btnLL"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true">
    <Button 
        android:id="@+id/testbutton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Save" />

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel"/>
  </LinearLayout>
    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/LstPeriodOptions"
        android:layout_above="@id/btnLL" />

</RelativeLayout>
like image 166
codeMagic Avatar answered Oct 14 '22 11:10

codeMagic


Try out as below to set your button in LinearLayout and set it below your ListView:

      <LinearLayout
        android:id="@+id/laytbtns"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/LstPeriodOptions" >
        <Button
            android:id="@+id/testbutton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="Save"/>
        <Button
             android:id="@+id/cancelButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Cancel" />
    </LinearLayout>
like image 39
GrIsHu Avatar answered Oct 14 '22 13:10

GrIsHu