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

1

6

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?

Oversexed answered 7/5, 2014 at 6:19 Comment(2)
A suggestion:you can create a separate layout for listview and include the footer in the listview xml file.Then I am sure that your footer will be shown even the listview is empty.Hackbut
@Hackbut Yeh but main.xml + footerview.xml have lots of content so i have applied setOnTouchListener to listview so that it will scroll with listview.Oversexed
E
4

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.

Explain answered 7/5, 2014 at 9:11 Comment(5)
Its not working mate,listview_followers.setEmptyView(footerview);Oversexed
View footerview = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)) .inflate(R.layout.footer_social, null, false);Oversexed
@Prince make sure to inflate footerView before inflating ListViewExplain
Sorry but not getting you.Oversexed
Working like a charm :D :DOversexed

© 2022 - 2024 — McMap. All rights reserved.