ListView.addFooterView () does not work if there are no items in the list item

I am adding a footer to the list using

View footerview = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_social, null, false); listview_followers.addFooterView(footerview); 

Here is my list in xml

 <ListView android:id="@+id/listview_followers" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> 

And footerview xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnSend_social" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@drawable/botao_bgg" android:text="SEND" android:textColor="#ffffff" android:textSize="20dp" android:textStyle="bold" /> </LinearLayout> 

footerview.xml was successfully added if there are elements in the list. If there are no items in the list, footerview is also not displayed.

Can anyone help with this?

+6
source share
1 answer

I do not think that it is possible to add a footer to an empty ListView . Although the ListView can use an empty View , which can be displayed when there are no items in the ListView . There are now two ways to do this:

1

 RelativeLayout empty = (RelativeLayout) getLayoutInflater().inflate( R.layout.empty_view, null); ((ViewGroup)list.getParent()).addView(empty); list.setAdapter(adapter); 

2

 RelativeLayout empty = (RelativeLayout) getLayoutInflater().inflate( R.layout.empty_view, (ViewGroup) list.getParent()); list.setEmptyView(empty); 

here empty is of type View , so it can be LinearLayout or RelativeLayout , which can be inflated using LayoutInflater at run time.

NOTE. make sure you install the adapter after adding an empty view.

Hope this helps.

+4
source

All Articles