Riskhan
Riskhan

Reputation: 4470

how to align a set of button to bottom of the screen in android

In my layout there are four sections respectively a header, an editable control, a list and a set of button. I want to keep the buttons bottom of the screen. I make too many changes in the layout to force the buttons to bottom. But nothing happened. Please provide instructions to make what i needed. I am posting 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

Upvotes: 7

Views: 36192

Answers (6)

Ganpat Kaliya
Ganpat Kaliya

Reputation: 898

try this.

<?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 display button at bottom.

Upvotes: 0

Akbari Dipali
Akbari Dipali

Reputation: 2183

Relative layout is the best way to do it as Karan_Rana suggested, but if you dont want to use relative layout try this one:

<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 parent and set weightSum to it and apply the same layout_weight to the layout you want in fullscreen. And the layout which you want in bottom(save and cancel) apply layout_weight to 0.

This works well!

Upvotes: 2

Sanket Pandya
Sanket Pandya

Reputation: 1095

Try out this code by making relative layout as parent layout and android:layout_alignParentBottom="true" to your 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>

Upvotes: 1

Sanober Malik
Sanober Malik

Reputation: 2805

Make the Parent of the LinearLayout containing the button as relative and set the property of linear layout containing two buttons as android:layout_alignParentBottom="true". You can also set the gravity of the Linear Layout to Bottom.

Try this:

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

Upvotes: 9

Rishabh Bhardwaj
Rishabh Bhardwaj

Reputation: 831

You have to use the parent layout as relative and then use the property android:layout_alignParentBottom="true" to sublayout.

Upvotes: 2

Karan_Rana
Karan_Rana

Reputation: 2823

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

Upvotes: 6

Related Questions