Display "No Item" message in ListView

Android

Android Problem Overview


I've created some composie UIs in my android apps and there are some ListView controls -among other controls- inside a view. Because of this, I have used "Activity" as my activity base class.

Now I need to display a simple message like "No Item" when the ListView that is bound to my adapter is empty. I know this is possible when using ListActivity but I'm not sure what's the best approach for this?

Android Solutions


Solution 1 - Android

You can have an empty view without a ListActivity! The correct method is as follows

First add an 'empty view' to your layout XML below your list

...
<ListView 
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

<TextView
    android:id="@+id/empty"
    android:text="Empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    />
...

Next override the onContentChanged method of your activity and set the empty view of your list to your empty view:

@Override
public void onContentChanged() {
    super.onContentChanged();

    View empty = findViewById(R.id.empty);
    ListView list = (ListView) findViewById(R.id.list);
    list.setEmptyView(empty);
}

That's it! Android will take care of hiding/showing the list and empty view when you update the adapter.

The Magic

Deciding whether the empty view is shown or not is handled by the superclass of ListView, AdapterView. AdapterView registers a DataSetObserver on the set adapter so it is notified whenever the data is changed. This triggers a call to checkFocus in AdapterView which contains the following lines

if (mEmptyView != null) {
    updateEmptyStatus((adapter == null) || adapter.isEmpty());
}

and sets the empty view visibility based on whether the adapter is empty or not.

Solution 2 - Android

You're looking for the empty view of a ListActivity:

ListActivity

If you're using ListView you can use the method setEmptyView():

setEmptyView

Solution 3 - Android

Just combine your ListView with TextView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
	android:id="@+id/list"
	android:layout_height="fill_parent"
	android:layout_width="fill_parent" />
<TextView
	android:id="@+id/list_empty"
	android:text="No Item"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_gravity="center"/>
</LinearLayout>

Then check the count of items an chanche visibility on ListView accordingly:

	ListView lv = (ListView)findViewById(R.id.list);
	lv.setVisibility((adapter.isEmpty())?View.GONE:View.VISIBLE); 

If you are using Custom Adapter, you can do this in the overridden notifyDataSetChanged method.

Solution 4 - Android

You can use Toast Message for this..

Check the Count of the Adapter value by adapter.getCount()

if(Adapter.getCount()!=0){
      List.setAdapter(Adapter);	
}else{
     Toast.makeText(YourActivityName.this, "No Items Available",Toast.LENGTH_SHORT).show();
}

Solution 5 - Android

For the layout code by Joseph, you need to edit the @+id/list and @+id/empty to @android:id/***, like:

<ListView 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

<TextView
    android:id="@android:id/empty"
    android:text="Empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    />

This way, you even don't need to override the onContentChanged() function.

Solution 6 - Android

The easiest way to achieve this was using a ListFragment instead of a ListActivity. ListFragment has the following convenience method:

setEmptyText("My no items message...");

Besides, using a ListFragment class has other advantages. For example, the possibility to combine it with the new AppCompat library (which you cannot do with ListActivity because you have to extend from ActionBarActivity).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHadi EskandariView Question on Stackoverflow
Solution 1 - AndroidJoseph EarlView Answer on Stackoverflow
Solution 2 - AndroidRoflcoptrExceptionView Answer on Stackoverflow
Solution 3 - AndroidGrAndView Answer on Stackoverflow
Solution 4 - AndroidVenkyView Answer on Stackoverflow
Solution 5 - AndroidFenjin WangView Answer on Stackoverflow
Solution 6 - AndroidargenkiwiView Answer on Stackoverflow