tags:

views:

605

answers:

1

In an Android ListActivity, I am adding a button as a footer in a ListView. How can I apply layout formatting to this button like centering it and/or width=fill_parent?

I have tried a few things such as using setLayoutParams(), but have not gotten it to work - the footer always disappears when I try anything.

Here is the basic code I am working with

closeButton = new Button(this);
closeButton.setText(getResources().getString(R.string.title_closeprocess));
closeButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {               
            closeProcess();
        }

   });

getListView().addFooterView(closeButton);   

*update

Based on Christopher's answer, here is the footer view that I am adding to my listview, and the activity code that adds it to the listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/closebutton_layout"    
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toptable" 
    android:layout_width="fill_parent"
    android:stretchColumns="*" 
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" 
    android:layout_height="wrap_content">
    <TableRow>          
        <Button android:id="@+id/closebutton" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="Skip Question"
        android:gravity="center" 
        android:paddingTop="16dip">
    </Button>           
    </TableRow>         
</TableLayout>
</RelativeLayout>



ListView listView = getListView();

    closeButtonView = getLayoutInflater().inflate(R.layout.closebutton_layout, listView, false);
    closeButton = (Button)closeButtonView.findViewById(R.id.closebutton);       
    closeButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            closeQuestion();                
        }

    });
listView.addFooterView(closeButtonView);
+1  A: 

Does it have to be a footer and scroll with the list? Otherwise you could just add it to the bottom in your XML layout, e.g.:

<LinearLayout
    android:orientation="vertical"
    android:weightSum="1">
  <ListView
      android:id="@id/android:list"
      android:layout_width="fill_parent"
      android:layout_weight="1" />
  <Button
      android:id="@+id/btn_close"
      ... />
</LinearLayout>

Alternatively, does placing your Button inside a LinearLayout, then adding that as a footer help?


Edit:
My brain really wasn't switched on earlier.

You can define the footer in a separate layout XML file, then inflate that and add it as your footer as normal. e.g.:

ListView lv = getListView();
View footer = getLayoutInflater().inflate(R.layout.list_footer, lv, false);
lv.addFooterView(footer);
Christopher
Thanks for your help. Yes, it does have to be a footer and scroll with the list - I can't have it taking up real estate all the time.As for your second suggestion... since I have to add this footer programmatically, I am not sure how to add it into a LinearLayout and subsequently add THAT layout as a footer. How is that to be done?
Sorry, I don't know what I was talking about earlier (though you can add any `View` as a footer so could do `linearLayout.add(button)`). Anyway, I've updated my post with a proper solution. You can define your footer layout in good old XML and then inflate it and add it to your footer.
Christopher
Thank you so much for your update - that is exactly what I was missing. THanks. I have updated my question because I am still a bit confused on the proper syntax of how to assign the OnClickListener to the Button in my footer view now that it is embedded in a layout ... Did I set up my footer view correctly?
Hey - I updated my question again with the latest version of my code. It works perfect. Thanks again! I am surprised I needed such a big footer layout just for the button, but when I put the button in a simple linear layout it was not expanding to fill the listview.