Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android addHeaderView disappears when no items in ListView

I am using addHeaderView to add a view item to the top of a ListView. I also have a TextView to display a message saying there are no items in the list.

Here is the layout:

<ListView android:id="@android:id/list"     android:layout_width="fill_parent"     android:layout_height="wrap_content"/>            <TextView     android:id="@android:id/empty"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:text="@string/list_empty"     android:gravity="center"     android:textAppearance="?android:attr/textAppearanceMedium" /> 

And the Java code:

final ListView listView = getListView(); final View view = getLayoutInflater().inflate(R.layout.list_item_add,             listView, false); listView.addHeaderView(view, null, true); 

When there are items in the ListView then the header is shown but if I delete all items in the list (except the header view) then the header view disappears.

I would like the header view to be visible in the list view whether there are items in the list or not.

Thanks,

like image 941
skyfoot Avatar asked Feb 14 '11 00:02

skyfoot


1 Answers

Remove the @android:id/empty view from your layout, or override/subclass your adapter to return false from isEmpty()

like image 105
superfell Avatar answered Oct 02 '22 14:10

superfell