How to show header of ListView when its empty

10,860

Solution 1

So this is how I solved it. I had a custom adapter which was connected to the listview. When it found that it had zero items to display. It would add a fake empty view as an item to the list.

Solution 2

I found a simple solution for this problem. If there's no elements for list and you are not adding the adapter, just add this:

mListView.setAdapter(null);

and the header will appear. It's easier than adding empty / fake item to the list.

Solution 3

maybe you will like this Android addHeaderView disappears when no items in ListView

override/subclass your adapter to return false from isEmpty()

Share:
10,860
MarutiB
Author by

MarutiB

Updated on June 15, 2022

Comments

  • MarutiB
    MarutiB almost 2 years

    I am developing the following screen

    enter image description here

    The fourboxes next to each other are buttons. On clicking of the buttons I am changing the adapter of the listview.

    Since the content above the listview took up lot of space I made the whole thing as an header and added it via code.

       myPollsList = (ListView) findViewById(R.id.listArea);
    myPollsList.addHeaderView(getLayoutInflater().inflate(R.layout.profile_listview_header, null));
    

    Now I want to show some view when the list is empty. But if I do that then the header also goes away.

    I am using this in a Activity and not a ListActivity. Any suggestions or workarounds for showing the header even when the list is empty ?

    EDIT: Refer to my earlier question here ListView not getting space to show content on smaller screens . This is where someone suggested me to solve the problem by putting it as a header

  • MarutiB
    MarutiB over 10 years
    Will that reduce the height of the top layouts ?
  • MarutiB
    MarutiB over 10 years
    That is not what I want. I want the height to be the same across all devices. That is why I chose the header route so that the height of the header remains fixed. And the rest of list view can be scrolled down
  • sandy
    sandy over 10 years
    as a proportion your all 3 views will be same size like 1:1:2 and also it will work great on any device just try it. cheers
  • MarutiB
    MarutiB over 10 years
    Nope. This is not the effect I wanted. Thanks for the help :)
  • Adrian Sicaru
    Adrian Sicaru almost 9 years
    Aghhh... All this hacks we have to do to make something that should be basic functionality work! It drives me crazy!
  • Dayanand Waghmare
    Dayanand Waghmare over 6 years
    thanks its work for me after adding these lines after addHeaderView() method