Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 801
Riskhan Avatar asked Mar 16 '13 06:03

Riskhan


1 Answers

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>

like image 180
Sanober Malik Avatar answered Sep 28 '22 09:09

Sanober Malik