Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an empty ListView's background image?

How yo set listview background like this. I want to appear when the number of record 0

enter image description here

like image 943
david Avatar asked Jun 06 '11 11:06

david


2 Answers

There is special method in ListView - setEmptyView(). You can find examples of using it here or here.

Upd: second link is unavailable now. Here is quote from article:

When you set a ListView’s “empty view” programmatically, you can end up scratching your head as to why your empty view actually doesn’t appear when the list is empty.
If this happens, then what you forgot is that you must manually add your empty view to your view hierarchy, cos ListView won’t do it for you. Although it’s obvious when you think about it, the documentation doesn’t mention this detail, and Googling shows at least one person had the problem.

Here’s the code with the lines that it’s all too easy to forget at numbers 4 and 5…

TextView emptyView = new TextView(context);
emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
emptyView.setText(“This appears when the list is empty”);
emptyView.setVisibility(View.GONE);
((ViewGroup)list.getParent()).addView(emptyView);
list.setEmptyView(emptyView);
like image 161
Sergey Glotov Avatar answered Sep 27 '22 18:09

Sergey Glotov


Just set a background image at the parent layout and then set the color of the ListView to fully transparent:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    style="@style/Main" android:background="@drawable/background">
    <ListView android:cacheColorHint="#00000000" .../>
</LinearLayout>
like image 43
Franziskus Karsunke Avatar answered Sep 27 '22 18:09

Franziskus Karsunke