You are on page 1of 5

9/24/2017 How to show a button at the end of an Android ListView - Stack Overflow

Learn, Share, Build


Each month, over 50 million developers come to Stack Overflow to Google Facebook
learn, share their knowledge, and build their careers. OR

Join the worlds largest developer community.

How to show a button at the end of an Android ListView

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>

android listview

edited Mar 14 '12 at 22:20 asked Mar 5 '10 at 12:21


Pops UMAR
16.3k 26 109 144 29.2k 85 181 256

2 Like this: blog.maxaller.name/2010/05/ ? Thomas Ahle Jun 28 '11 at 19:30

Check this stackoverflow.com/a/22319421/1128854 Zahid Habib Apr 24 '14 at 6:56

9 Answers

https://stackoverflow.com/questions/2386593/how-to-show-a-button-at-the-end-of-an-android-listview 1/5
9/24/2017 How to show a button at the end of an Android ListView - Stack Overflow
You may want to use ListView#addFooterView() to add a View at the bottom of the
ListView .

edited Mar 14 '12 at 22:53 answered Mar 5 '10 at 15:47


Pops Diego Torres Milano
16.3k 26 109 144 44.6k 6 66 97

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 Mar 9 '10 at 10:27

Thanks God!!!! dtmilano needs UP votes!!! Mateus Sep 5 '12 at 22:23

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

edited Oct 18 '13 at 6:10 answered Apr 10 '12 at 5:06


Sachin Gurnani
3,577 7 35 47

2 I don't understand why this answer has been ignored. It worked great for me, thanks! I just made a small
tweak to the ListView height in my edit which helped display lists that have only a few items. Splash Apr
17 '12 at 15:51

1 Out of all the solutions i looked out for a similar problem i was facing... this solution works BEST... thank u
so much! Zeba May 18 '12 at 8:14

1 This answer was very helpful ! and working perfectly Anjula Oct 16 '13 at 18:29

1 in my case, my button gets deformed to occupy the rest of the screen Thomas Dec 6 '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

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

edited Jun 28 '11 at 19:21 answered Jun 27 '11 at 21:49


takrl Pablo Johnson
4,918 3 45 63 562 7 18

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

https://stackoverflow.com/questions/2386593/how-to-show-a-button-at-the-end-of-an-android-listview 2/5
9/24/2017 How to show a button at the end of an Android ListView - Stack Overflow
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

edited Mar 5 '10 at 14:28 answered Mar 5 '10 at 12:35


Janusz skyman
93.8k 90 265 347 1,729 10 15

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 Mar 5 '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 5 '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 Mar 5 '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 5 '10 at 13:33

Edited answer to have both solutions. skyman Mar 5 '10 at 14:10

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

answered Mar 11 '14 at 7:50


SAndroidD
691 10 27

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"

https://stackoverflow.com/questions/2386593/how-to-show-a-button-at-the-end-of-an-android-listview 3/5
9/24/2017 How to show a button at the end of an Android ListView - Stack Overflow
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>

answered Jan 16 '15 at 10:28


Arunendra
982 13 10

Much simpler way than the actual marked solutions JPM Mar 27 '15 at 18:31

2 This way the button is shown always below it, which might not be the desired solution. vantesllar May 19
'15 at 16:21

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

answered May 17 '14 at 22:02


Beto
38 7

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>

answered Mar 5 '10 at 14:58

https://stackoverflow.com/questions/2386593/how-to-show-a-button-at-the-end-of-an-android-listview 4/5
9/24/2017 How to show a button at the end of an Android ListView - Stack Overflow
Richard Szalay
60.4k 14 136 189

still no hope while doing this button does not displays neither layout i have increased layout height but still
not visible. UMAR Mar 5 '10 at 16:03

hi...me too have facing same kind of problem,let me help to fix this issue. MGSenthil Sep 6 '10 at 9:52

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

answered May 23 '11 at 19:59


Snicolas
29.3k 10 80 150

https://stackoverflow.com/questions/2386593/how-to-show-a-button-at-the-end-of-an-android-listview 5/5

You might also like