Professional Documents
Culture Documents
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:
<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
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 .
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
<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
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
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
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
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
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);
list.addFooterView(footerLayout);
Example :
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>
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
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:
<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>
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 :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
@Override
public int getCount() {
return super.getCount()+1;
}//met
};
https://stackoverflow.com/questions/2386593/how-to-show-a-button-at-the-end-of-an-android-listview 5/5