Home » Android » Android ListView : default text when no items

Android ListView : default text when no items

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

How to&Answers:
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.

Answer:

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>

Answer:

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

Answer:

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));