Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView.addFooterView() not working if no items in listview

I am adding footer to listview using,

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

Here is my listview 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 successfully added if there are items in listview. If there are no items in listview, footerview is also not displayed.

Can anybody help about this?

like image 247
Hardik Joshi Avatar asked May 07 '14 06:05

Hardik Joshi


1 Answers

I dont think that its possible to add a footer to an empty ListView. Though ListView can use an empty View which can is displayed when there are no items in the ListView. now there are 2 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 a LinearLayout or RelativeLayout which can be inflated using LayoutInflater during runtime.

NOTE: make sure you set the adapter after adding the empty view.

Hope it helps.

like image 63
SMR Avatar answered Nov 12 '22 09:11

SMR