Showing empty view when ListView is empty
Asked Answered
A

11

140

For some reason the empty view, a TextView in this case, always appears even when the ListView is not empty. I thought the ListView would automatically detect when to show the empty view.

<RelativeLayout android:id="@+id/LinearLayoutAR"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <ListView android:id="@+id/ARListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></ListView>
    <ProgressBar android:id="@+id/arProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"></ProgressBar>
    <!-- Here is the view to show if the list is emtpy -->
    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="No Results" />
</RelativeLayout>

How can I hook up the empty view properly?

Argue answered 22/9, 2010 at 16:40 Comment(0)
I
137

It should be like this:

<TextView android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="No Results" />

Note the id attribute.

Intellectualism answered 22/9, 2010 at 16:48 Comment(9)
I changed the XML to the following, but still I get the same resultsArgue
you have to be using a ListViewActivity for it to work this way.Intellectualism
oh I see, my ListView is actually inside of a ViewFlipper. Is there another way I can present the view when the list is empty?Argue
you have implement the logic yourself, take a look at the sourcecode for ListViewActivity to see how they do it, or when you make your query check to see if it is empty, if so set your text view to VISIBLE, otherwise set it to GONEIntellectualism
using the textview when you are using a custom adapter is not working.Euterpe
Could we possibly get @jjoe64 's answer below accepted as correct? In cases such as this, when the ListView is not part of a ListActivity, this the correct (and quickest) way to hook up a View to be automatically displayed when a list is empty. (as is the case with the original question) All that a ListActivity does is automatically call this method with a view defined in the XML with id android:id/emptyHaloid
@CarlosP My answer was accepted because it was more relevant to his original question given the code he pasted. Thanks for the downvote though.Intellectualism
@schwiz false! you don't need a ListViewActivity to use the empty functionality.Quaver
@schwiz How to set single empty view for multiListview?Hildy
T
189

When you extend FragmentActivity or Activity and not ListActivity, you'll want to take a look at:

ListView.setEmptyView()

Telic answered 26/5, 2011 at 8:31 Comment(1)
any idea how to do that in app inventor?Hargrave
I
137

It should be like this:

<TextView android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="No Results" />

Note the id attribute.

Intellectualism answered 22/9, 2010 at 16:48 Comment(9)
I changed the XML to the following, but still I get the same resultsArgue
you have to be using a ListViewActivity for it to work this way.Intellectualism
oh I see, my ListView is actually inside of a ViewFlipper. Is there another way I can present the view when the list is empty?Argue
you have implement the logic yourself, take a look at the sourcecode for ListViewActivity to see how they do it, or when you make your query check to see if it is empty, if so set your text view to VISIBLE, otherwise set it to GONEIntellectualism
using the textview when you are using a custom adapter is not working.Euterpe
Could we possibly get @jjoe64 's answer below accepted as correct? In cases such as this, when the ListView is not part of a ListActivity, this the correct (and quickest) way to hook up a View to be automatically displayed when a list is empty. (as is the case with the original question) All that a ListActivity does is automatically call this method with a view defined in the XML with id android:id/emptyHaloid
@CarlosP My answer was accepted because it was more relevant to his original question given the code he pasted. Thanks for the downvote though.Intellectualism
@schwiz false! you don't need a ListViewActivity to use the empty functionality.Quaver
@schwiz How to set single empty view for multiListview?Hildy
C
120

As appsthatmatter says, in the layout something like:

<ListView android:id="@+id/listView" ... />
<TextView android:id="@+id/emptyElement" ... />

and in the linked Activity:

this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.emptyElement));

Does also work with a GridView...

Condensed answered 13/4, 2012 at 23:24 Comment(3)
After at least an hour of trying various things, and having nothing work (showing the "empty" text in addition to data in the listview), this worked for me when using a ListView in an Activity instead of extending ListActivity. Thank You Eddy!Uxorious
Should i make the TextView hidden or something ?Transfuse
@Harsha android:visibility="gone"Alister
B
44

I tried all the above solutions.I came up solving the issue.Here I am posting the full solution.

The xml file:

<RelativeLayout
    android:id="@+id/header_main_page_clist1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:paddingBottom="10dp"
    android:background="#ffffff" >

    <ListView
        android:id="@+id/lv_msglist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/divider_color"
        android:dividerHeight="1dp" />

    <TextView
        android:id="@+id/emptyElement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="NO MESSAGES AVAILABLE!"
        android:textColor="#525252"
        android:textSize="19.0sp"
        android:visibility="gone" />
</RelativeLayout>

The textView ("@+id/emptyElement") is the placeholder for the empty listview.

Here is the code for java page:

lvmessage=(ListView)findViewById(R.id.lv_msglist);
lvmessage.setAdapter(adapter);
lvmessage.setEmptyView(findViewById(R.id.emptyElement));

Remember to place the emptyView after binding the adapter to listview.Mine was not working for first time and after I moved the setEmptyView after the setAdapter it is now working.

Output:

enter image description here

Beeswing answered 28/1, 2015 at 9:1 Comment(1)
this worked for me after adding android:layout_below="@+id/lv_msglist" for emptyElementSarazen
S
16

I highly recommend you to use ViewStubs like this

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

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

    <ViewStub
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout="@layout/empty" />
</FrameLayout>

See the full example from Cyril Mottier

Sobriquet answered 2/1, 2014 at 7:32 Comment(0)
P
8
<ListView android:id="@+id/listView" ... />
<TextView android:id="@+id/empty" ... />
and in the linked Activity:

this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.empty));

This works clearly with FragmentActivity if you are using the support library. Tested this by building for API 17 i.e. 4.2.2 image.

Promissory answered 13/8, 2013 at 5:43 Comment(0)
H
7

Activity code, its important to extend ListActivity.

package com.example.mylistactivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.example.mylistactivity.R;

// It's important to extend ListActivity rather than Activity
public class MyListActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylist);

        // shows list view
        String[] values = new String[] { "foo", "bar" };

        // shows empty view
        values = new String[] { };

        setListAdapter(new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                android.R.id.text1,
                values));
    }
}

Layout xml, the id in both views are important.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- the android:id is important -->
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <!-- the android:id is important -->
    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="i am empty"/>
</LinearLayout>
Hap answered 1/1, 2013 at 16:6 Comment(0)
E
7

Just to add that you don't really need to create new IDs, something like the following will work.

In the layout:

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/empty"
    android:text="Empty"/>

Then in the activity:

    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setEmptyView(findViewById(android.R.id.empty));
Encyclopedia answered 12/2, 2015 at 18:44 Comment(0)
S
4

I had this problem. I had to make my class extend ListActivity rather than Activity, and rename my list in the XML to android:id="@android:id/list"

Son answered 25/4, 2012 at 17:58 Comment(0)
V
2

A programmatically solution will be:

TextView textView = new TextView(context);
textView.setId(android.R.id.empty);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText("No result found");
listView.setEmptyView(textView);
Vanderhoek answered 29/4, 2019 at 8:29 Comment(0)
C
-3

First check the list contains some values:

if (list.isEmpty()) {
    listview.setVisibility(View.GONE);
}

If it is then OK, otherwise use:

else {
     listview.setVisibility(View.VISIBLE);
}
Counterpoise answered 6/11, 2014 at 7:28 Comment(4)
would you care to further elaborate what is a list?Midgut
It's an ArrayList<xyz>Counterpoise
Object[] response = Utility.parseStationresponce(object); int statusCode = (Integer) response[0]; @SuppressWarnings("unchecked") ArrayList<Stations> list = (ArrayList<Stations>) response[2];Counterpoise
Well good , I suggest you put that as part of your answer than comments. This is important to get others to comprehend the specific context of what you are writing. Furthermore, it is also best if you can relate your answer to OP's question. Finally, Welcome to SO.Midgut

© 2022 - 2024 — McMap. All rights reserved.