15

I'm trying attach a footer, that is fixed and always visible, to the bottom of a ListFragment.

I'm currently doing it like this:

@Override public void onActivityCreated(Bundle savedInstanceState) {

    // ...

    adapter = new MyAdapter(getActivity(), R.layout.list, dataList);

    ListView list = getListView();
    View footer = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_add, null, false);

    list.addFooterView(footer);
    setListAdapter(adapter);
}

While this code does produce a view at the bottom of the list, it doesn't really do what I want:

First, I need the footer to be FIXED, i.e., visible on the screen regardless of where the list is scrolled. With this solution, the footer is only visible when the screen is scrolled to the bottom of the list.

Second, I need the footer to appear even when the list is EMPTY. In this solution, the footer is not visible when the list is empty.

What is the best way to get a fixed footer (in my case, a button) to always appear below a ListFragment or ListActivity?

Thanks!

gcl1
  • 3,970
  • 11
  • 36
  • 55

1 Answers1

41

You can do that in the xml layout:

<RelativeLayout>

    <Button android:id="@+id/footer" android:layout_alignParentBottom="true"/> 
    <ListView android:id="@android:id/list" android:layout_above="@id/footer"> <!-- the list -->

</RelativeLayout>

This layout will be used in the onCreateView method of the fragment.

user
  • 85,380
  • 17
  • 189
  • 186
  • Would this work even if I'm using a ListFragment, rather than a plain old Fragment? Or would I have to convert it to a Fragment? – gcl1 Sep 10 '12 at 14:57
  • @gcl1 A `ListFragment` is a fragment for which the View is a simple `ListView` with the id `android.R.id.list`. You can still use the `ListFragment`, you just need to override the `onCreateView` method and return the layout from my answer. Th rest of your code from the current `ListFragment` will remain the same. – user Sep 10 '12 at 14:59
  • I modified R.layout.footer_add to have a RelativeLayout with the ListView and the footer Button. Does the overridden onCreateView method get the layout using findViewById on R.layout.footer_add, or by inflating it? I'm getting an error either way. – gcl1 Sep 10 '12 at 15:54
  • 2
    @gcl1 The `onCreateView` should be like this: `@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.the_layout, container, false); }` . – user Sep 10 '12 at 15:58
  • WORKS, awesome! My mistake was leaving in the dynamic call to addFooterView(). Thanks a million! – gcl1 Sep 10 '12 at 16:02
  • Quick follow up question. Your solution works great, but the rows of the list are added starting at the bottom of the page, just above the footer button. Is there a way to keep the button on the bottom, but have the list rows attach starting at the top? – gcl1 Sep 10 '12 at 16:28
  • @gcl1 I'm not sure I understand. See if you didn't set the transcript mode(the method `setTranscriptMode` from the `ListView` widget). Or add the attribute `android:layout_alignParentTop="true"` for the `ListView` in the layout file. – user Sep 10 '12 at 16:34
  • Looks like I forgot the android:layout_alignParentTop="true" on the ListView. Works now, thanks again!! – gcl1 Sep 10 '12 at 16:39
  • listview width and height should be match_parent...I was facing issue here..thanks – Raj Trivedi Oct 15 '14 at 07:31