Android - two buttons on one line filling the entire width

I have a little problem defining relative layout. I have a scroll list view and two buttons that are always visible at the bottom of the list. I just wish my button had 50% 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 enter buttons in a linear layout and give a gravity value = 1 with a width = 0dp, but in this case the ListView will disappear. could you help me?

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> 
+7
android layout relativelayout
source share
3 answers

Have you LinearLayout this way because it should work. Pay attention to all property changes. Since I do not know how it was, I cannot point out all 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> 
+30
source share

Try as shown below to set your button in LinearLayout and set it below the 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> 
+4
source share

Try it.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/LstPeriodOptions" android:layout_above="@id/testbutton" /> <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> </LinearLayout> 
+2
source share

All Articles