41

I want to show a button at the end of an Android list view. How can I achieve this?

I don't want to stick it to the activity bottom using alignparentbottom="true". Using layout_below does not work for me either.

My current XML:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_bg">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="false"
            android:cacheColorHint="#ff6a00"
            android:divider="#ff8f40"
            android:dividerHeight="1px" />

    </LinearLayout>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
            android:layout_height="50sp"
        android:background="#676767"
        android:orientation="vertical">

        <Button
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:id="@+id/btnGetMoreResults"
            android:layout_marginLeft="10px"
            android:text="Get more" />

    </RelativeLayout>

</RelativeLayout>
Pops
  • 30,199
  • 37
  • 136
  • 151
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

9 Answers9

74

You may want to use ListView#addFooterView() to add a View at the bottom of the ListView.

Pops
  • 30,199
  • 37
  • 136
  • 151
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • yes thanks man. finally created dynamic button and then using getListView().addFooterView ( DynamicButton); now i can see button at the end of list view. – UMAR-MOBITSOLUTIONS Mar 09 '10 at 10:27
35

I do it like this fixed button at the buttom of the screen

   <RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/btn_New" >
    </ListView>

    <Button
        android:id="@+id/btn_New"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="20dp"
        android:text="@string/New"
        android:width="170dp"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

if ur using linearLayout then assign android:layout_weight="1" to the listview and dont assign weight for button it works

Sachin Gurnani
  • 2,444
  • 7
  • 36
  • 45
  • 1
    in my case, my button gets deformed to occupy the rest of the screen – Thomas Dec 06 '13 at 15:32
  • ScrollView surrounding ListView (which scrolls too) isn't a good idea. You might find it starts to behave oddly. – Ghoti Mar 10 '14 at 17:51
  • @Thomas, my button was floating over the ``. If this is your case, try removing from your ` – Junior Mayhé Feb 28 '16 at 14:53
15

You could do something like this:

final Button btnAddMore = new Button(this);
btnAddMore.setText(R.string.art_btn_moreIssues);
exArticlesList = (ExpandableListView) this.findViewById(R.id.art_list_exlist);
exArticlesList.addFooterView(btnAddMore);
takrl
  • 6,356
  • 3
  • 60
  • 69
Pablo Johnson
  • 1,044
  • 15
  • 21
7

1 If you want to add Button as the last element of the list view

You must create custom ListAdapter for your ListView which will create a view with a Button in the getView method. You should decide how to return your custom view for the last element, you can hardcode it (return element count +1 in getCount method and return custom view in getView when position > element count) or you can add element to the structure you will be taking data from (Array, Cursor etc.) and check if field of element have certain value

2 If you want to add element below list view

You should use android:layout_width attribute and make ListView and "empty" TextView (you should use it to show users that list is empty and View rendering is completed) layout_weight greater than buttons layout_weight

Check how it's done in Transdroids search Activity http://code.google.com/p/transdroid/source/browse/trunk/res/layout/search.xml

Community
  • 1
  • 1
skyman
  • 2,422
  • 17
  • 16
  • i am not talking about putting button in list view i want to place only single button at the bottom of list view thats it. i think you have not understood my question properly. – UMAR-MOBITSOLUTIONS Mar 05 '10 at 12:56
  • To add any data to the ListView, you must create View that will be added. Object controlling what is shown in the ListView is its Adapter. Standard Adapters (SimpleAdapter, SimpleCursorAdapter, ArrayAdapter) can create only items from single layout, so you have to create your own Adapter (you cannot just add item to ListView like for ex. to swing JTable). – skyman Mar 05 '10 at 13:15
  • thanks for your reply. but i am confused related to you are using word adding in list view. in my case i dont want to add button to list view but display after it where list view data ends. then why it has relationship with list view to make it custom? – UMAR-MOBITSOLUTIONS Mar 05 '10 at 13:24
  • Ok so maybe I've really not understood. Do you want the button to be "in" the ListView (as the last element) or below the ListView and at the bottom of the screen? – skyman Mar 05 '10 at 13:33
  • Edited answer to have both solutions. – skyman Mar 05 '10 at 14:10
6

Use Footer view to list-view it work.

list_layout.xml :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:cacheColorHint="#ff6a00"
        android:divider="#ff8f40"
        android:dividerHeight="1px" />

</LinearLayout>

footerview.xml :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
    <Button
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:id="@+id/btnGetMoreResults"
        android:layout_marginLeft="10px"
        android:text="Get more" />
</FrameLayout>

and in Activity.java

    list=(ListView)findViewById(R.id.list);

    FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.footerview,null);
    btnPostYourEnquiry = (Button) footerLayout.findViewById(R.id.btnGetMoreResults);

    list.addFooterView(footerLayout);
SAndroidD
  • 1,745
  • 20
  • 33
4

Simply Provide "layout_weight = 1" of ListView.

Example :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:cacheColorHint="#ff6a00"
        android:divider="#ff8f40"
        android:layout_weight="1"
        android:dividerHeight="1px" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ff8f40"
        android:padding="20dp"
        android:text="Click"
        android:textColor="#FFFFFF"
        android:textSize="22sp"
        />
</LinearLayout>
Arunendra
  • 2,034
  • 26
  • 22
3

I came here looking for an answer first but found it somewhere else...

It's really easy, you just need to put weight 1 to the list inside a linear layout, the other textviews/buttons/etc don't need to have any weight value.

Here is an example: https://bitbucket.org/generalplus/android_development/src/5a892efb0551/samples/ApiDemos/res/layout/linear_layout_9.xml

Beto
  • 73
  • 1
  • 2
  • 8
0

You could, of course, use a custom adapter and specify a footer item. But you could probably also get away with putting it at the bottom of a ScrollView and have the ListView stretch vertically to the content:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_bg">

  <ScrollView
      android:layout_height="fill_parent"
      android:layout_width="fill_parent">

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            >

      <ListView android:id="@+id/android:list"
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content"
          android:drawSelectorOnTop="false" 
          android:cacheColorHint="#ff6a00"
          android:divider="#ff8f40"
          android:dividerHeight="1px"
          />

      <RelativeLayout 
          android:layout_width="fill_parent"
          android:layout_height="50sp"
          android:background="#676767"
          android:orientation="vertical">

        <Button android:layout_width="100px"
            android:layout_height="wrap_content"
            android:id="@+id/btnGetMoreResults"
            android:layout_marginLeft="10px"
            android:text="Get more" />

      </RelativeLayout>

    </LinearLayout>

  </ScrollView>

</RelativeLayout>
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
0

Well... details of implementations are not so easy if you want to create a smooth "add more" button at the end of the list. So here is some code :

myArrayAdapter = new ArrayAdapter<Show>(this, android.R.layout.simple_list_item_1, myList) {

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {

        if( position >= super.getCount()  )
            return buttonMore ;

        MyNormalView view = null; 
        if (convertView == null || convertView instanceof Button )
            view = new MyNormalView(getContext());
        else
            view = (MyNormalView) convertView;

        //customize view here with data
        return view;
    }

    @Override
    public int getCount() {
       return super.getCount()+1;
    }//met
};
Snicolas
  • 37,840
  • 15
  • 114
  • 173