31

I've a ListView that can have 0 custom items inside (like "My Downloads").

Is there anyway to show a default text "No download yet" ?

Thanks !

EDIT : here is my solution,

TextView emptyView = new TextView(getApplicationContext());
emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
emptyView.setTextColor(R.color.black);
emptyView.setText(R.string.no_purchased_item);
emptyView.setTextSize(20);
emptyView.setVisibility(View.GONE);
emptyView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

((ViewGroup)getListView().getParent()).addView(emptyView);
getListView().setEmptyView(emptyView);

alt text

2
  • Why the View.GONE property setting? Commented Oct 24, 2016 at 13:50
  • 1
    @medonja because setEmptyView(emptyView) method doesn't work properly if you don't add the emptyView to the view hierarchy first but doing so the emptyView would be always visible. So you have to make it GONE and this works well since what setEmptyView() does is just changing the emptyView's visibility according to the empty state of the list. Commented Jan 1, 2017 at 21:00

4 Answers 4

37
public void setEmptyView (View emptyView)

Since: API Level 1
Sets the view to show if the adapter is empty

Called on your ListView instance.

E.g. mListView.setEmptyView(someView);

You can build view on the fly, or inflate one from the xml.

Sign up to request clarification or add additional context in comments.

Comments

16

Unless I misread your question, you can also just specify any view element with an ID of "@android:id/empty" in your layout and it will be taken care automatically for you. For example:

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

<LinearLayout android:id="@android:id/empty"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center">

    <TextView android:text="@string/no_messages"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        style="@style/textBoldLight">
    </TextView>

</LinearLayout>

2 Comments

i did not think of giving id of empty to linear layout to center "no items found" good thinking.
This will only work for ListActivity. You will need to use Alex's approach for other Activity classes.
1

There's another approach BaseAdapter has method isEnabled(). You need to create you own adapter which will return for certain elements isEnabled(int position) as false. In this case those element will be unselectable. It's easy to modify it in a way that when list empty just add one disabled element.

But for sure method described above by Alex Orlov is better

Comments

0

If you're using Activity instead of ListActivity and want to set the gravity of the empty to center (both horizontally and vertically), you should use these:

In your layout:

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

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

    <LinearLayout
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no_contacts_add_one">
        </TextView>

    </LinearLayout>


</LinearLayout> 

In your activity:

ListView contacts = (ListView) view.findViewById(R.id.contacts);
contacts.setEmptyView(view.findViewById(android.R.id.empty));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.