Android: set empty view to a list view
Asked Answered
T

6

17
 <TextView
            android:id="@android:id/empty"
            style="@style/FacebookFont"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/no_result_found"
            android:textColor="@color/white" />

this is the xml code for empty view. I am setting empty view as follows:

    list.setAdapter(adapter);
    list.setEmptyView(findViewById(android.R.id.empty));

I have the items in the listview even then also before listview is populated the empty view is shown. I don't want that empty view to be shown when listview contains some item.

Please help.

Turnkey answered 11/4, 2013 at 13:18 Comment(0)
A
15

if you are using ListActivity then no need to set the empty view manually. Remove list.setEmptyView(findViewById(android.R.id.empty)); from your code.

Change your layout like below then it's automatically add EmptyView if list has no item:

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

    <ListView android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00FF00"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"/>

     <TextView android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:text="No data"/>

</LinearLayout>
Asomatous answered 11/4, 2013 at 13:26 Comment(3)
Can you show me you full activity code and layout file code??? Add these all in your questionAsomatous
sure, but how do you get a hold of that TextView (or whatever view) if you need to do something to it, there's no real id associated to it, you traverse the layout programatically?Dichotomous
Good tip, it also works if you're using ListFragment. No need for the extra large fonts though :PAcreage
D
11

You have to set the empty view before setting the adapter, if your MainActivity is not extending ListActivity.

Try this

list.setEmptyView(findViewById(android.R.id.empty));
list.setAdapter(adapter);

Sorry for the late answer!

Alternate method: Make your activity extend from ListActivity. And add a textview to your list layout with id android.R.id.empty

Draw answered 27/9, 2013 at 6:24 Comment(0)
L
5

Try this

    View view = getLayoutInflater() .inflate(<resource id of the empty view>, null);
    ViewGroup viewGroup= ( ViewGroup)list.getParent();
    viewGroup.addView(view);
    list.setEmptyView(view); 
    list.setAdapter(adapter);
Lifelong answered 14/2, 2014 at 16:27 Comment(0)
H
5

There are some correct answers, however, I think this is the best approach I've found:

View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null);
addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView
listView.setEmptyView(emptyView);
listView.setAdapter(adapter);
Healey answered 23/4, 2014 at 21:10 Comment(0)
C
0

Whatever @dhawalSodhaParmar has explained is correct. But there is a confusion when you look at the way the answer is put down.

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

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SampleActivity">

    <ListView
        android:id="@+id/list"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:divider="@null"
        android:dividerHeight="0dp" />

    <!-- Empty view is only visible when the list has no items. -->
    <TextView
        android:id="@+id/empty_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="NO DOCUMENTS AVAILABLE!"
        android:textColor="#525252"
        android:textAppearance="?android:textAppearanceMedium"
        android:visibility="gone"/>

</RelativeLayout>

Suppose if you already have a list view and adapter backing the same with data. And assuming everything is working fine, whoever wants to implement this empty list view screen has to start like below: Step 1: Modify the existing Activity XML file which has the list view with the one I have used here. You can use either a Linear Layout or Relative Layout, but preferably Relative because of the flexibility. And a textView like I have written.

Step 2: Go to your Activity Java file, and after your setContentView

ListView emptyListView = (ListView) findViewById(R.id.list);
// set your adapter here
// set your click listener here
// or whatever else
emptyListView.setEmptyView(findViewById(R.id.empty_text_view));

That's it, now run and you are set!

Coulter answered 2/8, 2017 at 10:26 Comment(0)
B
0
// first of all create a global variable of Text view 
// which is displayed when the list is empty

private TextView mEmptyStateTextView;
    
//then in onCreate(Bundle savedInstanceState)
/Setting empty text view

mEmptyStateTextView=(TextView) findViewById(R.id.emptyview);

view.setEmptyView(mEmptyStateTextView);  

//then in onLoadfinished method

mEmptyStateTextView.setText(R.string.no_earthquakes);
Bought answered 3/1, 2021 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.