Search code examples
androidlistviewlayoutfooter

ListView footer width


I have a problem with an footer for the listview(I can't center the footer).

I'm using a footer for an endless list.

The problem I'm having is that the footer is not stretched to match the list width(instead is somehow using wrap_content).

This is the footer layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/footer_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:padding="10dp" >

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Loading more..." />

</LinearLayout>

And this is how I add the footer to the listview:

View footerView;

...     

// Get the custom layout for footer
        footerView = getActivity().getLayoutInflater().
                inflate(R.layout.list_footer_load_more, getListView(), false);
        LinearLayout footerViewLayout = (LinearLayout) footerView.findViewById(R.id.footer_layout);
        // Adding custom view to ListView at footer
        getListView().addFooterView(footerViewLayout, null, false);

And this is how the final result looks like: (I can't post images on stackoverflow, but here is an outside link to the image) http://s21.postimg.org/3zkd7pic7/listview_footer_problem.png

I tried everything to center the footer but nothing worked. I'm hoping someone can help me, because this is stressing me out for some time.

Thank you.


Solution

  • Change your footer layout(R.layout.list_footer_load_more) to:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/footer_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical" />
    
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:layout_gravity="center_vertical"
                android:text="Loading more..." />
    
        </LinearLayout>
    </RelativeLayout>  
    

    Change your code to:

    View footerView;
    
    ...     
    
    // Get the custom layout for footer
    footerView = getActivity().getLayoutInflater().
                inflate(R.layout.list_footer_load_more, null);
    RelativeLayout footerViewLayout = (RelativeLayout) footerView.findViewById(R.id.footer_layout);
        // Adding custom view to ListView at footer
        getListView().addFooterView(footerViewLayout, null, false);