Reputation: 587
I have the following XML. I'm trying to make a static button underneath my ScrollView. I've tried to set weights, set the button to be below the scrollview, etc. Can someone give me a way that I can get the button to stay at the bottom and the scrollview only take up the middle of the screen?
<?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="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menuButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Items" />
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/categories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories" />
</LinearLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contentScroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/menuButtons">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow android:layout_marginTop="5dip"
android:layout_marginBottom="5dip" >
<ImageView
android:src="@drawable/thumbdrive"/>"
<TextView
android:layout_column="1"
android:text="Thumb Drives"
android:padding="3dip"
android:textSize="20dip"/>
</TableRow>
<TableRow android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<ImageView
android:src="@drawable/laptop"/>
<TextView
android:layout_column="1"
android:text="Laptops"
android:padding="3dip"
android:textSize="20dip"/>
</TableRow>
<TableRow android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<ImageView
android:src="@drawable/sdcard"/>
<TextView
android:layout_column="1"
android:text="SD Cards"
android:padding="3dip"
android:textSize="20dip"/>
</TableRow>
<TableRow android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<TextView
android:layout_column="1"
android:text="Other"
android:padding="3dip"
android:textSize="20dip"/>
</TableRow>
<TableRow android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<TextView
android:layout_column="1"
android:text="Other"
android:padding="3dip"
android:textSize="20dip"/>
</TableRow>
<TableRow android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<TextView
android:layout_column="1"
android:text="Other"
android:padding="3dip"
android:textSize="20dip"/>
</TableRow>
<TableRow android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<TextView
android:layout_column="1"
android:text="Other"
android:padding="3dip"
android:textSize="20dip"/>
</TableRow>
<TableRow android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<TextView
android:layout_column="1"
android:text="Other"
android:padding="3dip"
android:textSize="20dip"/>
</TableRow>
<TableRow android:layout_marginTop="5dip"
android:layout_marginBottom="5dip">
<TextView
android:layout_column="1"
android:text="Other"
android:padding="3dip"
android:textSize="20dip"/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Submit New Item"
android:layout_below="@id/contentScroller"/>
</RelativeLayout>
Upvotes: 30
Views: 33824
Reputation: 100398
Use a RelativeLayout
. Start with the Button
on the bottom, and position the ScrollView
above the Button
.
Relative Layout - Android Developers
<RelativeLayout
(...)>
<LinearLayout android:id="@+id/ll1"
android:layout_alignParentTop="true"
(...)/>
<Button android:id="@+id/button"
android:layout_alignParentBottom="true"
(...)/>
<ScrollView
android:layout_above="@id/button"
android:layout_below="@id/ll1"
(...)/>
</RelativeLayout>
Something like this. Written out of my head, so some errors may occur.
Upvotes: 45
Reputation: 6594
I know this is really late but there is a better solution. The problem with the RelativeLayout approach and aligning the buttons to the bottom of the relative layout is that it forces the layout height to essentially be the same as fill_parent (or match_parent).
The proper way to do this is as follows:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true">
<!-- Your Scrollview content goes here -->
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="Button Text Goes Here" />
</LinearLayout>
The key is to set the height to 0 and then give it a layout weight of 1... From what I can tell from reading up on the layout sizing process, giving it a size of 0 and a layout weight causes it to hold off on sizing the view until after it has processed all the children in the layout... It then comes around on a second pass and is able to size the scrollview properly.
Upvotes: 66
Reputation: 2798
place the button in a relativeLayout and align it to parent bottom:
<RelativeLayout android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:orientation="horizontal"
android:layout_height="50px">
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Submit New Item"
android:layout_below="@id/contentScroller"/>
</RelativeLayout>
Upvotes: 0