31

I have a scrollable ListView with items (like in http://developer.android.com/resources/tutorials/views/hello-listview.html). I am using an ArrayAdapter for the items and use it as a parameter in setListAdapter. Now I would like to add a button at the bottom of the screen, which does not scroll with the list. Could someone give me some hints or post a code snippet how it could possibly be done?

Alex P.
  • 407
  • 1
  • 5
  • 14

3 Answers3

111

If your activity extends ListActivity then you need something like this:

<LinearLayout android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <ListView android:id="@android:id/list"
              android:layout_height="0dip"
              android:layout_width="match_parent"
              android:layout_weight="1" />

    <Button android:id="@+id/btn" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

Notice that the listview has a layout_weight set to 1. That will keep the button fixed in its place at the bottom.

starball
  • 20,030
  • 7
  • 43
  • 238
Srichand Yella
  • 4,218
  • 2
  • 23
  • 24
  • 1
    if you are using `weight` and the `orientation` is `vertical` , so you should have : `android:layout_height="0dp"` – Houcine Apr 22 '13 at 15:57
  • 1
    Houcine, you are right. In one of the recent updates, Google changed the ADT so it throws a warning when using 'fill_parent'. A lot has changed since I answered the question in Jun 2011. I changed my answer and made it current. – Srichand Yella Apr 22 '13 at 21:12
  • After adding `android:layout_marginTop="20px"` to my button, the ListView doesn't show up anymore, and the button sits at the top of the screen. I removed the `marginTop`, but it's still behaving like this. Is there a safe way to get a tiny space between the Button and ListView? Am I doing this right, or could my code be to blame? – Krummelz Aug 06 '13 at 07:31
  • This only works if you have match_parent on Layout...doesn't work for wrap – JPM Jan 08 '15 at 18:45
  • Great solution for wrapping dialogs with a static button at the bottom. – TalMihr Dec 22 '16 at 09:07
15

you can use a RelativeLayout to fix the button at the bottom of your layout , and add your listView above it like this :

<RelativeLayout android:layout_width="match_parent"
         android:layout_height="match_parent">

      <Button android:id="@+id/btn" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>

     <ListView 
            android:id="@android:id/list"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
               android:layout_above="@id/btn" />
</RelativeLayout>
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • I generated the ListView by extending the class by `ListActivity` and using `getListView()` to get it. But if I now use a layout and use `setContentView` to set it, the application throws the following exception: `Your content must have a ListView whose id attribute is 'android.R.id.list'` – Alex P. Jun 19 '11 at 17:39
  • i think u should custom your ListView when you get it !! can you add your code and the logcat too ? – Houcine Jun 19 '11 at 18:03
  • 1
    name your list android:id="@android:id/list" if you want to use getListView() – mkso Jun 19 '11 at 18:12
  • good solution, but ListView will situated bottom while contains few items. – Michael Kazarian Apr 28 '16 at 12:54
  • This doesn't work. If the list fills up, the button remains below the screen – Yash Jain Jun 25 '20 at 20:04
  • @YashJain: I think that even if the list fills up, it will always be above the button element (which is aligned at the bottom of the parent layout). – Houcine Jun 26 '20 at 01:34
1

My solution based on Houcline's solution but ListView always above Button

<CheckBox
    android:id="@+id/chbRemoveIfUninstall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"/>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_above="@id/chbRemoveIfUninstall"/>

Community
  • 1
  • 1
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25