views:

1742

answers:

3

I want to show a button at the end of an android list view
How can I achieve this?
i dont want to stick it to the activity bottom using alignparentbottom="true", layout_below does not work for me either.
But i want to show it at the end of list view

Any help would be appricated here comes my code.

<?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>
+3  A: 

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

skyman
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.
UMMA
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
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?
UMMA
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
Edited answer to have both solutions.
skyman
yes below at the bottom.
UMMA
A: 

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
still no hope while doing this button does not displays neither layout i have increased layout height but still not visible.
UMMA
hi...me too have facing same kind of problem,let me help to fix this issue.
MGSenthil
+2  A: 

You may want to use [ListView#addFooterView][1] to add a View at the bottom of the ListView.

[1]: http://developer.android.com/intl/de/reference/android/widget/ListView.html#addFooterView(android.view.View, java.lang.Object, boolean)

dtmilano
yes thanks man.finally created dynamic button and then using getListView().addFooterView ( DynamicButton);now i can see button at the end of list view.
UMMA