How yo set listview background like this. I want to appear when the number of record 0
5 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);

- 1
- 1

- 20,200
- 11
- 84
- 98
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>

- 4,948
- 3
- 40
- 54
Read the documentation of the ListActiviy. You can define a view which will automatically shown when the list is empty and has not items. The view for the empty list got to have the id android:id/empty
.
So no need to play around with the background.

- 27,355
- 15
- 87
- 125
You can set a drawable as background with ListView.setBackgroundDrawable()

- 5,554
- 1
- 24
- 30
-
Just to note, since this question still comes up as a top result, this method was deprecated in API level 16. – JustSomeQuickGuy Aug 10 '15 at 15:54
You need to check before passing array
/arraylist
into adapter
,if the length of array
/arraylist
is 0 then add this image to your main layout.

- 19,522
- 20
- 117
- 184

- 3,693
- 5
- 37
- 59