Reputation: 77984
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>
Upvotes: 42
Views: 60685
Reputation: 2174
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>
Upvotes: 4
Reputation: 73
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
Upvotes: 3
Reputation: 1755
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);
Upvotes: 6
Reputation: 2444
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
Upvotes: 35
Reputation: 69218
You may want to use ListView#addFooterView() to add a View
at the bottom of the ListView
.
Upvotes: 74
Reputation: 1072
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);
Upvotes: 15
Reputation: 38168
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
};
Upvotes: 0
Reputation: 84754
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>
Upvotes: 0
Reputation: 2472
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
Upvotes: 7