How to align a set of buttons at the bottom of the screen in android

There are four sections in my layout, respectively, the title, the editable control, the list and the set of buttons. I want to hold the buttons at the bottom of the screen. I make too many changes in the layout to force the buttons from below. But nothing happened. Please provide instructions to do what I need. I place the layout also

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- heading for basic settings --> <TextView android:id="@+id/setup_macroheading" style="@style/txtHeading" /> <View android:layout_width="fill_parent" android:layout_height="20dp"/> <!-- macro name --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="20dp" android:paddingRight="20dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:layout_weight="1" android:textColor="@color/white" android:text="Macro Name"/> <EditText android:id="@+id/setup_macroname" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:maxLength="12" android:inputType="textCapCharacters" android:singleLine="true"/> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="20dp"/> <ListView android:id="@+id/lvMacroList" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- Save & Cancel button --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_gravity="bottom" android:orientation="horizontal"> <Button android:id="@+id/setup_macroSavebtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_weight="1" android:onClick="onSaveButtonClick" android:text="Save"/> <Button android:id="@+id/setup_macroCancelbtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:onClick="onCancelButtonClick" android:text="Cancel"/> </LinearLayout> </LinearLayout> </ScrollView> 

enter image description here

+7
source share
6 answers

Make the parent LinearLayout element containing the button as relative, and set the linear layout property containing the two buttons as android: layout_alignParentBottom = "true". You can also set the gravity of the linear layout at the bottom.

Try the following:

 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- heading for basic settings --> <TextView android:id="@+id/setup_macroheading" style="@style/txtHeading" /> <View android:layout_width="fill_parent" android:layout_height="20dp"/> <!-- macro name --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="20dp" android:paddingRight="20dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:layout_weight="1" android:textColor="@color/white" android:text="Macro Name"/> <EditText android:id="@+id/setup_macroname" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:maxLength="12" android:inputType="textCapCharacters" android:singleLine="true"/> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="20dp"/> <ListView android:id="@+id/lvMacroList" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <!-- Save & Cancel button --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_gravity="bottom" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp" > <Button android:id="@+id/setup_macroSavebtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_weight="1" android:onClick="onSaveButtonClick" android:text="Save"/> <Button android:id="@+id/setup_macroCancelbtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:onClick="onCancelButtonClick" android:text="Cancel"/> </LinearLayout> </RelativeLayout> 

+7
source

Take the main parent as Relative layout and give android:layout_alignParentBottom="true" button layout ...

+6
source

You should use the parent layout as relative, and then use the android: layout_alignParentBottom = "true" property for sub-launch.

+2
source

Relative layout is the best way to do this, as Karan_Rana suggested, but if you don't want to use a relative layout, try this option:

 <LinearLayout android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical"> <!-- heading for basic settings --> <TextView android:id="@+id/setup_macroheading" android:layout_width="match_parent" android:layout_height="wrap_content" /> <View android:layout_width="fill_parent" android:layout_height="20dp"/> <!-- macro name --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="20dp" android:paddingRight="20dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:layout_weight="1" android:textColor="@color/white" android:text="Macro Name"/> <EditText android:id="@+id/setup_macroname" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:maxLength="12" android:inputType="textCapCharacters" android:singleLine="true"/> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="20dp"/> <ListView android:id="@+id/lvMacroList" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> </ScrollView> <!-- Save & Cancel button --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_weight="0" android:layout_gravity="bottom" android:orientation="horizontal"> <Button android:id="@+id/setup_macroSavebtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_weight="1" android:onClick="onSaveButtonClick" android:text="Save"/> <Button android:id="@+id/setup_macroCancelbtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:onClick="onCancelButtonClick" android:text="Cancel"/> </LinearLayout> 

Here you need to use LinearLayout as the parent and set weightSum for it and apply the same layout_weight to the layout you want in full screen. And the layout you want at the bottom (save and cancel) applies layout_weight to 0.

It works well!

+2
source

Try this code by making the relative layout as the parent layout and android: layout_alignParentBottom = "true" on the button 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" > <!-- heading for basic settings --> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <View android:layout_width="fill_parent" android:layout_height="20dp" /> <!-- macro name --> <LinearLayout android:id="@+id/llTop" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:layout_weight="1" android:text="Macro Name" /> <EditText android:id="@+id/setup_macroname" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:inputType="textCapCharacters" android:maxLength="12" android:singleLine="true" /> </LinearLayout> <!-- Save & Cancel button --> </RelativeLayout> </ScrollView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:paddingLeft="20dp" android:paddingRight="20dp" > <Button android:id="@+id/setup_macroSavebtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_weight="1" android:onClick="onSaveButtonClick" android:text="Save" /> <Button android:id="@+id/setup_macroCancelbtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_weight="1" android:onClick="onCancelButtonClick" android:text="Cancel" /> </LinearLayout> 

+1
source

try it.

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" > <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp" android:text="Modify" /> <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginRight="2dp" android:text="Cancle" /> </LinearLayout> </RelativeLayout> 

for the display button below.

0
source

All Articles