Where to add setEmptyView


Not quite clear on if I need an xml layout for the listview or if i can manage all of this though code.

the book says that you can use this method or create a layout. can’t seem to find a way to reference listview in code. thanks! :question:


found it, but it does make me curious why i would want an xml layout for the ListView ???


Would you like to post your solution for the benefit of other people who have the same problem. Thanks.


Having a separate layout file for the listview is handy if you want it to appear within a frame of some kind.

See my answer elsewhere for more info on the empty view - this is a view that exists somewhere in your hierarchy that ListView will hide or show for you. If there is an empty view and your ListView has no data, it will hide itself and show the empty view. When it does have data, it will hide the empty view, and show itself.


Yea if you want to create both a textview with a message like (“There are no crimes.” AND you want to add a button for adding a crime ALL in the same “empty list” view stub. You should create a list layout file like so:

[code]<?xml version="1.0" encoding="utf-8"?>


    android:layout_height="match_parent" >
    <!--  android:id="@android:id/empty" !-->


In order for the listview adapter to know how to switch between an empty list view stub and the actual list view when crimes are added, you have to use specific id values:
for the populated list add a listview child to the root viewgroup (a framelayout in this case) with an id value of:

then for the empty list (unpopulated list) I wrapped a textview and a button inside of a framelayout viewgroup with the id value of:

Then back in the crimelistFragment.java file within onCreateView, I added/edited the code as follows:

public View onCreateView(LayoutInflater inflater, ViewGroup parent,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.empty_list_item_stub, parent, false);
		ListView view = (ListView)v.findViewById(android.R.id.list);

The list adapter takes care of the switching for you based on the two IDs …list and …empty. Hope this helps.


[quote=“PCfrustration”]Yea if you want to create both a textview with a message like (“There are no crimes.” AND you want to add a button for adding a crime ALL in the same “empty list” view stub. You should create a list layout file like so:


My understanding is defining “@android:id/empty” & “@android:id/list” in the XML is a complete solution in itself. Post adding these ids & inflating in onCreateView(); a call to setEmptyView() will make no difference and is redundant. Can someone please correct me if I am wrong?

Also, can’t figure out the solution which uses the setEmptyView() and not the XML solution, can someone who has cracked it post some pointers? Thanks!


I have the same notion about adding android:id in xml file: once you have it there, android will automatically chooses between the two lists. Could someone explain that, please.


Just need to inflate the layout xml file.

The following two lines are not required.
ListView view = (ListView)v.findViewById(android.R.id.list);


I was confused with this as well. Played around a bit, and I think I figured out what the book is trying to say:

  1. You can make the IDs for the listview and empty view “list” and “empty”, respectively, then they’ll switch automatically.
  2. You can make your own view (using code or xml perhaps? I just used xml.) as the empty view, ID it with what you want, and use the method setEmptyView() using the custom ID to make android use that view instead of “empty”.

Please correct me if I’m wrong, just trying to get a better understanding.