Search code examples
androidandroid-listviewandroid-listfragment

Fixed and always visible footer below ListFragment


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!


Solution

  • 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.