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=
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
};
You may want to use ListView#addFooterView() to add a View
at the bottom of the ListView
.
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>
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
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
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);